Creating and using a .pgpass file

A .pgpass file will allow you to use postgres CLI tools such as psql and pg_dump without having to manually enter a password — You can use the programs from scripts without having to run them as a non password protected user.

First, create the .pgpass file

#nano /root/.pgpass 

According to the official doccumentation the format of the file is as follows:


The file supports the use of # tags for comments and * to match on wildcards. Here is an example of mine:


Enter your database information and save.

Next, set the permissions. If they are not 600 Postgres will ignore the file.

#chmod 600 /root/.pgpass

Now how do we make postgres use it? A quick scan through psql man page shows:

-w, --no-password 
 Never issue a password prompt. If the server requires password authentication and a password is not available by other means such as a .pgpass file, the connection attempt will fail. This option can be useful in batch jobs and scripts where no user is present to enter a password.

Here are some usage examples:
#psql -d postgres -U postgres -w
#pg_dump -U postgres -w -Fc