7

I want to restore my database postgres using pg_restore in command line and i want to put the password of user directly in the command, I tried this command but doesn't work

pg_restore -h localhost -p 5432 -U postgres -W 123 -d my_db my_backup.backup

but the following message is displayed

pg_restore: too many arguments on the command line (the first being "-d")
Fares Izem
  • 139
  • 1
  • 2
  • 8

1 Answers1

14

Set the password as an environment variable: set "PGPASSWORD=123" You can set other arguments this way as well: https://www.postgresql.org/docs/current/static/libpq-envars.html

-W is to force a password prompt, not to supply a password directly, thats why it says there are too many arguments.

Putting it all together:

set "PGPASSWORD=123"
pg_restore -h localhost -p 5432 -U postgres -d my_db my_backup.backup

Update: Thanks @aschipfl, I initially had incorrect quoting on set

mike.k
  • 3,287
  • 1
  • 11
  • 17
  • 1
    The question didn't ask for it, but using a password file is more secure. – Laurenz Albe Jun 21 '18 at 15:34
  • 2
    When using `set PGPASSWORD="123"`, the quotes are part of the variable value; write `set "PGPASSWORD=123"` instead to avoid that... – aschipfl Jun 21 '18 at 16:54
  • If you need to run this from a script you need to export the environment variable. `export PGPASSWORD='123'` – Libert Mar 18 '22 at 13:46