PostgreSQL .pgpass File for Passwordless Login
Table of Contents
1. What is .pgpass?
.pgpass is a plain text file in the home directory of the OS user. When psql, pg_dump, pg_basebackup or any other client needs a password, it reads this file automatically, so you are not prompted.
-
- It is a client-side file. Nothing changes on the server.
- It is used only when the server asks for a password (
scram-sha-256ormd5inpg_hba.conf). It is ignored withtrustorpeer. - Passwords are stored in clear text, so the file must have permission
0600.
2. File Format
One connection per line, five fields separated by colons:
hostname:port:database:username:password
- Use
*in the first four fields to match anything. - For replication connections, write
replicationin the database field. - Write a literal
:in a password as\:and a backslash as\\. - Lines starting with
#are comments.
3. Configure and Test
Run as the OS user that runs the client (here postgres):
[postgres@pg01 ~]$ cat > ~/.pgpass << 'EOF'
localhost:5432:postgres:postgres:MyStrongPassw0rd
EOF
[postgres@pg01 ~]$ chmod 600 ~/.pgpass
[postgres@pg01 ~]$ ls -l ~/.pgpass
-rw-------. 1 postgres postgres 49 Nov 12 09:15 /home/postgres/.pgpass
Test with -w (no password prompt). If the login works, .pgpass was used:
[postgres@pg01 ~]$ psql -h localhost -p 5432 -U postgres -d postgres -w -c "select current_user;"
current_user
--------------
postgres
(1 row)
4. File Permissions (0600)
If group or others have any access, the file is ignored:
[postgres@pg01 ~]$ chmod 644 ~/.pgpass
[postgres@pg01 ~]$ psql -h localhost -U postgres -d postgres -w
WARNING: password file "/home/postgres/.pgpass" has group or world access; permissions should be u=rw (0600) or less
psql: error: connection to server at "localhost" (::1), port 5432 failed: fe_sendauth: no password supplied
# Fix
[postgres@pg01 ~]$ chmod 600 ~/.pgpass
The file must also be owned by the user who runs the client (chown postgres:postgres ~/.pgpass).
5. Example: Wildcards
The first matching line (top to bottom) is used, so put specific lines first and wildcard lines last.
# specific entry first
pgdb01:5432:salesdb:appuser:AppPass#1
# postgres user on every database of this server
pgdb01:5432:*:postgres:PostgresPass#2
# one read-only user on any host and database
*:5432:*:report_ro:ReadOnlyPass#3
6. Example: Replication
On the standby (pg02), add an entry for the primary with replication as the database:
[postgres@pg02 ~]$ cat > ~/.pgpass << 'EOF'
192.168.2.101:5432:replication:replicator:ReplPass123
EOF
[postgres@pg02 ~]$ chmod 600 ~/.pgpass
# pg_basebackup now needs no password
[postgres@pg02 ~]$ pg_basebackup -h 192.168.2.101 -U replicator -D /pgData/17/data -R -P -w
The standby’s primary_conninfo should not contain password=.... It will use .pgpass instead:
[postgres@pg02 ~]$ grep primary_conninfo /pgData/17/data/postgresql.auto.conf
primary_conninfo = 'user=replicator host=192.168.2.101 port=5432'
7. Example: pg_dump with cron
A cron job has no terminal, so it cannot answer a password prompt. Add a .pgpass line and use -w so the job fails fast if the entry is missing:
[postgres@pg01 ~]$ echo 'localhost:5432:salesdb:backup_user:BackupPass456' >> ~/.pgpass
[postgres@pg01 ~]$ cat > /home/postgres/scripts/backup_salesdb.sh << 'EOF'
#!/bin/bash
/usr/pgsql-17/bin/pg_dump -h localhost -p 5432 -U backup_user -w -Fc salesdb \
-f /pgBackup/salesdb_$(date +%F).dump
EOF
[postgres@pg01 ~]$ chmod 750 /home/postgres/scripts/backup_salesdb.sh
[postgres@pg01 ~]$ crontab -e
0 2 * * * /home/postgres/scripts/backup_salesdb.sh >> /pgLog/backup_salesdb.log 2>&1
Cron must run as the OS user that owns .pgpass.
8. Use a Different File (PGPASSFILE)
# environment variable
[postgres@pg01 ~]$ export PGPASSFILE=/pgBackup/.pgpass_backup
[postgres@pg01 ~]$ chmod 600 $PGPASSFILE
To make it permanent, add the line to ~/.bash_profile with the export keyword. Without export, psql cannot see the variable and still asks for a password:
# ~/.bash_profile
export PGPASSFILE=/pgBackup/.pgpass_backup
[postgres@pg01 ~]$ . ~/.bash_profile
[postgres@pg01 ~]$ env | grep PGPASSFILE
PGPASSFILE=/pgBackup/.pgpass_backup
9. Alternative: Export PGPASSWORD
This is not .pgpass, but it also avoids the password prompt. Export the password in your session, then run the command:
[postgres@pg01 ~]$ export PGPASSWORD='MyStrongPassw0rd'
[postgres@pg01 ~]$ psql -h localhost -p 5432 -U postgres -d postgres -c "select current_user;"
current_user
--------------
postgres
(1 row)
# remove it when finished
[postgres@pg01 ~]$ unset PGPASSWORD
For a single command only, set it in front of the command (it is not kept in your session):
[postgres@pg01 ~]$ PGPASSWORD='MyStrongPassw0rd' psql -h localhost -U postgres -d postgres -c "select now();"
PGPASSWORDis checked before.pgpass. If it is set,.pgpassis not used.- The password is visible in your shell history and, on some systems, to other users. Use it only for quick tests and prefer
.pgpassfor regular use.