# How do I run psql without typing the password?

Put the password in ~/.pgpass with mode 0600 so psql stops asking. Why PGPASSWORD and a password in a URI leak, and how psql -w keeps a script from hanging.

Source: https://valakes.com/learn/psql-without-password-prompt/

Last updated: 2026-11-29

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.

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

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.

```console
$ 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:

```console
$ 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:

```console
$ 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:

```console
$ 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](https://valakes.com/learn/file-permissions/) explains the numbers. Set the mode and try again:

```console
$ 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:

```console
$ 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:

```console
$ 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:

```console
$ 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`:

```console
$ 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:

```console
$ 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`:

```console
$ 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](https://valakes.com/learn/ssh-port-forwarding/).

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](https://valakes.com/learn/ssh-keys/) shows, and keep the database role to what the work needs.

MySQL has its own file and its own warning; see [the MySQL article](https://valakes.com/learn/mysql-password-command-line-warning/).

## 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](https://valakes.com/guides/auto-fill-a-database-password-over-ssh/) shows the steps, and [the feature page](https://valakes.com/features/password-auto-fill/) 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.

## In Valakes

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](https://valakes.com/features/password-auto-fill/)

[Read the docs: Credentials and auto-fill](https://docs.valakes.com/credentials/)

## 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 Password File (.pgpass), PostgreSQL 16 manual, libpq](https://www.postgresql.org/docs/16/libpq-pgpass.html)
- [Environment variables, PostgreSQL 16 manual, libpq (PGPASSWORD and PGPASSFILE)](https://www.postgresql.org/docs/16/libpq-envars.html)
- [psql, PostgreSQL 16 manual (-w, -W and connection strings)](https://www.postgresql.org/docs/16/app-psql.html)
- [ps(1) on Ubuntu 24.04, the option -e](https://manpages.ubuntu.com/manpages/noble/man1/ps.1.html)

## Related articles

- [How do I generate an SSH key and put it on a server?](https://valakes.com/learn/ssh-keys/): Run ssh-keygen -t ed25519, then ssh-copy-id -i ~/.ssh/id_ed25519.pub user@host. The private key stays on your computer, the public key goes into ~/.ssh/authorized_keys on the server, and you log in without a password.
- [How do I let a user run one command with sudo?](https://valakes.com/learn/sudo-for-one-command/): Write a rule that names the command with its full path and exact arguments, put it in its own file under /etc/sudoers.d/, check it with visudo -cf, and install it with mode 440. Do not use wildcards in the arguments, and do not allow editors or any program that can start a shell.
- [How do I change permissions recursively on Linux?](https://valakes.com/learn/file-permissions/): Do not use one number for the whole tree. Run find site -type d -exec chmod 755 {} + for folders and find site -type f -exec chmod 644 {} + for files, then chown -R to set the owner and group. chmod -R 777 lets every account on the machine change your files.
- [How do I get rid of "Using a password on the command line interface can be insecure"?](https://valakes.com/learn/mysql-password-command-line-warning/): The warning appears because you gave the password as a value, as in -ps3cret, and then it sits in the command line where ps can show it. Move it into a [client] section of ~/.my.cnf with mode 600, or into a login path made by mysql_config_editor, and run mysql without it.
