How do I run psql without typing the password?

Put one line in ~/.pgpass in the form hostname:port:database:username:password and run chmod 600 on the file. psql then finds the password itself, and psql -w makes a script fail with an error instead of waiting for a prompt that nobody can answer.

Last updated

Commands checked on Ubuntu 24.04.5 LTS (the ubuntu:24.04@sha256:562111b88caa79c71391c0c4d3c2114a7d5c35915c20561fd893cb1e07929e6b container image), .

Create ~/.pgpass with one line for each database login, in the form hostname:port:database:username:password, and run chmod 600 on it. psql reads the file when the server asks for a password, so the prompt does not appear. Add -w to every psql command in a script. A missing or wrong password then ends the command with an error, where without it the script could sit at a prompt that nobody will answer.

The commands below ran on the system named in the check line, against a PostgreSQL 16 server on the same machine. Its role app has a password, and the server asks for it on TCP connections.

$ psql --version
psql (PostgreSQL) 16.15 (Ubuntu 16.15-0ubuntu0.24.04.1)

What the prompt does, and what -w does

With no password available, psql asks for one. Nobody is here to type, so timeout stops psql after three seconds:

$ timeout --foreground 3 psql -h 127.0.0.1 -U app appdb -c 'select current_user'; rc=$?; echo; echo "exit status $rc"
Password for user app:
exit status 124

-w tells psql never to prompt. The connection then fails at once:

$ psql -h 127.0.0.1 -U app appdb -w -c 'select current_user'; echo "exit status $?"
psql: error: connection to server at "127.0.0.1", port 5432 failed: fe_sendauth: no password supplied
exit status 2

psql has no option that takes the password as a value. -W only forces a prompt, even when the password could be found elsewhere. The error no password supplied comes back whenever psql has nothing to send, even when a password file exists but no line of it matches.

Write the ~/.pgpass file

On Linux and macOS the file is .pgpass in the home directory of the user who runs psql. The five fields are hostname:port:database:username:password. The first four may be *, which matches anything. Write a line for the server above:

$ echo '127.0.0.1:5432:appdb:app:s3cret-pass' > ~/.pgpass
$ stat -c '%a %n' ~/.pgpass
644 /home/dev/.pgpass
$ psql -h 127.0.0.1 -U app appdb -w -c 'select current_user'
WARNING: password file "/home/dev/.pgpass" has group or world access; permissions should be u=rw (0600) or less
psql: error: connection to server at "127.0.0.1", port 5432 failed: fe_sendauth: no password supplied

The mode is 644, so other accounts could read the password. The manual says that a file with more access than 0600 is ignored, and psql prints a warning to tell you. The permissions article explains the numbers. Set the mode and try again:

$ chmod 600 ~/.pgpass
$ psql -h 127.0.0.1 -U app appdb -w -c 'select current_user'
 current_user
--------------
 app
(1 row)

The other programs of the PostgreSQL client package read the same file. pg_dump is one:

$ pg_dump -h 127.0.0.1 -U app -w -s appdb > /dev/null; echo "exit status $?"
exit status 0

PGPASSFILE: a file in another place

PGPASSFILE names a different file, with the same format and the same mode rule. It suits one file per project, or a file that sits next to a script. From here on -tAc prints the bare value, without the table around it:

$ mv ~/.pgpass ~/app.pgpass
$ PGPASSFILE=~/app.pgpass psql -h 127.0.0.1 -U app appdb -w -tAc 'select current_user'
app

PGPASSWORD, and who can read it

PGPASSWORD gives the password for one command and needs no file. The manual does not recommend it. Some operating systems let other users see the environment of a process with ps. Here is the variable inside a running psql, with the file still moved away:

$ PGPASSWORD=s3cret-pass psql -h 127.0.0.1 -U app appdb -w -tAc 'select pg_sleep(5)' > /dev/null &
$ sleep 1; stat -c '%a %U' /proc/$(pgrep -f '^/usr/lib/postgresql/16/bin/psql' | head -1)/environ
400 dev
$ tr '\0' '\n' < /proc/$(pgrep -f '^/usr/lib/postgresql/16/bin/psql' | head -1)/environ | grep PGPASS
PGPASSWORD=s3cret-pass
$ wait

On this system the file /proc/<pid>/environ is readable only by its owner, so other accounts do not see the variable. The owner and root do, and so does every program the owner starts. The line you type to set it also lands in your shell history.

A password in a URI or in the command

psql accepts a connection string, such as postgresql://app:s3cret-pass@127.0.0.1/appdb, and the manual allows it in place of a database name. The password in it is part of the command line. Every account can list command lines with ps -e:

$ psql 'postgresql://app:s3cret-pass@127.0.0.1/appdb' -w -tAc 'select pg_sleep(5)' > /dev/null &
$ sleep 1; ps -eo args= | grep '[p]sql postgres'
/usr/lib/postgresql/16/bin/psql postgresql://app:s3cret-pass@127.0.0.1/appdb -w -tAc select pg_sleep(5)
$ wait

The shell history is the other place the password ends up. An interactive bash reads one command and writes its history file when it exits, as your own shell does:

$ HISTFILE=~/demo_history bash -i <<< "psql 'postgresql://app:s3cret-pass@127.0.0.1/appdb' -w -c 'select 1' > /dev/null" > /dev/null 2>&1
$ cat ~/demo_history
psql 'postgresql://app:s3cret-pass@127.0.0.1/appdb' -w -c 'select 1' > /dev/null

Leave the password out of the string and let .pgpass supply it: psql 'postgresql://app@127.0.0.1/appdb'.

Mistakes people make

The host in the file is not the host in the command. psql compares the host field with the host you gave, as text. A line for localhost does not match 127.0.0.1:

$ printf 'localhost:5432:appdb:app:s3cret-pass\n' > ~/.pgpass; chmod 600 ~/.pgpass
$ psql -h 127.0.0.1 -U app appdb -w -tAc 'select current_user'
psql: error: connection to server at "127.0.0.1", port 5432 failed: fe_sendauth: no password supplied
$ psql -h localhost -U app appdb -w -tAc 'select current_user'
app

The same rule applies to the port and the database. If you reach the database through a tunnel, the line needs the address and the local port that psql is given, as in the port forwarding article.

You write the line with echo and a real password. The command goes into your shell history. Use an editor for it, or clear the line from the history file.

You add -W out of habit. It makes psql prompt every time, even when the file has the line.

You put a line with * first. The first matching line wins, so the specific lines belong above it.

You forget that the file belongs to the account. Anyone who logs in as that account, or as root, can read the password. Give the account a login with a key, as the SSH keys article shows, and keep the database role to what the work needs.

MySQL has its own file and its own warning; see the MySQL article.

In Valakes

Valakes can keep the database password in its vault and bind it to psql for a host. When psql asks, Valakes checks that the prompt is genuine and sends the value, so you type no password and your history keeps a placeholder. The guide shows the steps, and the feature page lists the tools it knows. The Help says Valakes cannot hide a secret it does not know, such as a password in a .env file on the server. A .pgpass file is the same kind of file, so give it the mode above.

Valakes can bind a saved password to psql on a host and answer the password prompt itself, so you do not type the password, and it stays out of the command and out of your history. Feature: Password auto-fill

Questions

What if the password contains a colon or a backslash?

Escape each one with a backslash in the file, so a password ab:cd is written ab\:cd.

Which line does psql use when several match?

The first one that matches the connection. Put the specific lines first and any line with * after them.

Does this work on Windows?

The file is named pgpass.conf and sits in the postgresql folder under %APPDATA%. The manual says no permission check is made there.

References

The app is free.

Get notified at launch

macOS is coming to the MacĀ AppĀ Store. iOS and Android later.