how to use .pgpass file

· bergpb's blog

Using .pgpass file to authenticate with PostgreSQL databases

PostgreSQL has a lot of good features and I've learned about a new one very recently (yeah, that's a old feature introduced on version 7.4, that I don't know yet :`) ).

With .pgpass file, it's possible to store the credentials to connect with different databases without copy and paste the credentials everytime. Very useful to use with pgdump and pgrestore commands.

First you need to create a file called .pgpass on your $HOME directory and apply the correct permissions to it, as recommended on official docs:

touch ~/.pgpass
chmod 600 ~/.pgpass

Now we need to understand the structure of this file to add the credentials in the correct order:

hostname:port:database:username:password

The file support multiple entries, so you can add the connection details for different hosts, databases and so on. Also wildcards(*) and comments(#) are also supported.

Reference:

PostgreSQL Docs: The pg_hba.conf File

last updated: