Fix PostgreSQL password authentication failed for user
PostgreSQL password authentication failed for user hides five causes. The server log DETAIL line tells a wrong password, missing role, md5 hash or expiry apart.
psql: error: connection to server at "127.0.0.1", port 5432 failed: FATAL: password authentication failed for user "appuser"When a connection that matched a password line (scram-sha-256 or md5) fails, the client gets this single line whatever the reason. Besides a wrong password, it also appears when the role doesn't exist, the role has no password, an md5-stored password meets a scram line, or the password has expired. The real reason is only in the server log's DETAIL: line, so start there.
[1] Find the DETAIL: line right below it in the server log. Package installs usually log under /var/log/postgresql/; in Docker use docker logs <container>. This is the log for a wrong password.
FATAL: password authentication failed for user "appuser"DETAIL: Connection matched file "/etc/postgresql/lab/pg_hba.conf" line 2: "host all all 127.0.0.1/32 scram-sha-256"Connection matched … line 2 is the pg_hba.conf line that applied. Other causes put a different sentence there.
DETAIL: | Cause | Next |
|---|---|---|
Connection matched file … line N | Wrong password | [2] |
Role "ghost" does not exist. | No such role | Check the role name |
User "nopw" has no password assigned. | The role has no password | ALTER ROLE … PASSWORD |
User "oldapp" does not have a valid SCRAM secret. | md5-stored password + scram-sha-256 line | [3] |
User "appuser" has an expired password. | VALID UNTIL has passed | [4] |
A missing role also reaches the client as password authentication failed for user "ghost". If changing the password never helps, suspect this. Role naming rules are in Fix PostgreSQL role does not exist.
[2] Check which password the client actually sent. A stale ~/.pgpass makes psql use it without asking, and on failure psql adds a password retrieved from file "…" line saying so. First just read that role's line with grep appuser ~/.pgpass.
~/.pgpass holds passwords for other connections too, one per line, so don't overwrite it to test. Put a separate file in PGPASSFILE for that one command. This reproduction uses a test file with a deliberately stale value:
$ printf '127.0.0.1:5432:*:appuser:stale-pass\n' > /tmp/pgpass.test && chmod 600 /tmp/pgpass.test && PGPASSFILE=/tmp/pgpass.test psql -h 127.0.0.1 -U appuser -d postgres -c 'SELECT 1'psql: error: connection to server at "127.0.0.1", port 5432 failed: FATAL: password authentication failed for user "appuser"password retrieved from file "/tmp/pgpass.test"$ sed -i 's/stale-pass/<password>/' /tmp/pgpass.test && PGPASSFILE=/tmp/pgpass.test psql -h 127.0.0.1 -U appuser -d postgres -Atc 'SELECT current_user'appuserIf you see password retrieved from file, fix only that line in that file (before editing the real ~/.pgpass, copy it with its permissions: cp -p ~/.pgpass ~/.pgpass.bak). Delete the test file when done. PGPASSWORD, when set, is used before .pgpass. To reset a password, connect as an admin role and run \password appuser — it asks twice and keeps the value out of your shell history and the server log.
In a connection URL, percent-encode characters like @, :, / and # in the password. Left as-is, the URL is split in the wrong place.
$ psql 'postgresql://webapp:p@ss:word@127.0.0.1:5432/postgres' -c 'SELECT 1'psql: error: invalid integer value "word@127.0.0.1:5432" for connection option "port"$ psql 'postgresql://webapp:p%40ss%3Aword@127.0.0.1:5432/postgres' -Atc 'SELECT current_user'webappIn the test password p@ss:word, @ became %40 and : became %3A. This combination failed while parsing the address, before authentication. If your application builds the URL itself, check that it encodes the password part.
[3] For does not have a valid SCRAM secret, set the password again. Roles migrated from an old server, or created while password_encryption = 'md5', hold an md5 hash, and a scram-sha-256 line rejects them even with the right password.
$ psql -U postgres -c "SELECT rolname, left(rolpassword, 14) AS stored FROM pg_authid WHERE rolname IN ('appuser','oldapp')" rolname | stored ---------+---------------- appuser | SCRAM-SHA-256$ oldapp | md50f1178081dc(2 rows)A value starting with md5 is an md5 hash. In the same admin session, check that SHOW password_encryption is scram-sha-256; setting the same password again then stores it as SCRAM and the login passes.
postgres=# SHOW password_encryption;postgres=# \password oldapp\password asks for the new password twice and keeps it out of shell history and the server log. The test did the same reset with ALTER ROLE oldapp PASSWORD …, after which a TCP login with the same password got in as oldapp. For the check login, don't put the password on the command line either; use the prompt (-W) or .pgpass as in [2]. The other direction already works: the docs say an md5 line in pg_hba.conf automatically uses SCRAM for SCRAM-stored passwords. So reset the password rather than switching pg_hba.conf back to md5.
[4] For has an expired password, fix the expiry. Even a correct password is rejected once VALID UNTIL has passed.
$ psql -U postgres -Atc "SELECT rolname, rolvaliduntil FROM pg_roles WHERE rolname = 'appuser'"appuser|2020-01-01 00:00:00+00$ psql -U postgres -c "ALTER ROLE appuser VALID UNTIL 'infinity'" && PGPASSWORD='<password>' psql -h 127.0.0.1 -U appuser -d postgres -Atc 'SELECT current_user'ALTER ROLEappuserIf policy requires expiry, set a new date instead of infinity. VALID UNTIL applies only to password authentication; it does not block peer or trust logins.
[5] In Docker, remember that changing POSTGRES_PASSWORD does nothing to an existing volume. The variable is read only on the first start with an empty data directory. Get in with psql -U postgres inside the container (the image uses trust on the socket) and change it with \password postgres.
For Connection refused, which fails before any password check, see Fix PostgreSQL Connection refused; for Peer authentication failed on socket connections, see that guide.
Official references: Password Authentication · pg_hba.conf · The Password File (.pgpass) · libpq connection URIs and percent-encoding · CREATE ROLE — VALID UNTIL · Docker official image postgres
Check it with the pg_hba.conf rule checker tool ↗ — calculated in your browser.