KB: How to run pg_dump on a command line with a password

Linux

The "best practise" method is to specify the password in a file stored in your home folder:

mousepad ~/.pgpass
*:5432:*:username:password
chmod 0600 ~/.pgpass

Another method is to specify it on the command line:

PGPASSWORD="password" pg_dump -h localhost -p 5432 -U username mydatabase

Windows

The "best practise" method is to specify the password in a file stored in your profile folder:

notepad %APPDATA%\postgresql\pgpass.conf
*:5432:*:username:password

Another method is to specify it on the command line:

PGPASSWORD=password&& pg_dump -h localhost -p 5432 -U username mydatabase

If the version of pg_dump supports the "--dbname" argument then you can use this method:

pg_dump --dbname=postgresql://username:password@localhost:5432/mydatabase