Skip to content

password authentication failed: what the server log tells you

PostgreSQLtested on PostgreSQL 18.7

The connection matched a line in pg_hba.conf, but the login fails. For security reasons the client never learns the exact cause – the server log does: its DETAIL names the matching pg_hba.conf line and, if relevant, the problem with the stored password.

Symptom

PGPASSWORD=falsch psql "service=notes" -c 'SELECT 1'
psql: error: connection to server at "172.16.0.182", port 5432 failed: FATAL:
password authentication failed for user "notes_app"

The same message appears for a typo in the password and for a hash the method doesn't accept. Only the server can tell them apart.

Diagnosis

Read the line after the message in the server log. The DETAIL names the pg_hba.conf line that matched.

sudo grep -A1 'password authentication failed' \
  /var/log/postgresql/postgresql-18-main.log | tail -2
... notes_app@notes FATAL:  password authentication failed for user "notes_app"
... notes_app@notes DETAIL:  Connection matched file
"/etc/postgresql/18/main/pg_hba.conf" line 133: "host  notes  notes_app
172.16.0.0/24  scram-sha-256"

Causes

Wrong passwordPGPASSWORD beats .pgpass

If PGPASSWORD is set, libpq uses it instead of the entry in ~/.pgpass. A forgotten export from an earlier test is enough to get rejected.

MD5 hashOld MD5 hash, the rule requires scram-sha-256

Roles with an MD5 hash are still around after upgrades from old versions. The method scram-sha-256 in pg_hba.conf only allows SCRAM – the password is right, the login fails anyway. The server log says "does not have a valid SCRAM secret".

psql -c "SELECT rolname, left(rolpassword, 14) AS hash FROM pg_authid
  WHERE rolname LIKE 'tag03%';"
   rolname    |      hash
--------------+----------------
 tag03_report | SCRAM-SHA-256$
 tag03_alt    | md598d97dd2d87
(2 rows)
... tag03_alt@notes DETAIL:  User "tag03_alt" does not have a valid SCRAM secret.

Fix

For a typo: use the right password and check PGPASSWORD. For an MD5 hash: find roles with an old hash and set the password again – with password_encryption = scram-sha-256 a SCRAM hash is created.
psql -Atc "SELECT rolname FROM pg_authid WHERE rolpassword LIKE 'md5%'"
psql -c "ALTER ROLE tag03_alt PASSWORD 'lab-tag03'"
psql -Atc "SELECT left(rolpassword, 14) FROM pg_authid WHERE rolname = 'tag03_alt'"
tag03_alt
ALTER ROLE
SCRAM-SHA-256$

Don't: Falling back to md5 in pg_hba.conf. MD5 passwords are deprecated as of PostgreSQL 18 – the goal is scram-sha-256 everywhere and no md5 hashes left.

▶
Break it yourself and find it – free
PostgreSQL hands-on, day 3: configuration and access – reading and changing pg_hba.conf, SCRAM instead of MD5 and TLS with your own CA.
Go to day 3 →

FAQ

Why doesn't the client say what is wrong?

For security reasons it doesn't learn the cause. The server log names it in the DETAIL, along with the file and line of the matching rule.

What about "Peer authentication failed"?

Same layer – the login – but over the Unix socket with the peer method. It checks the Linux user at the socket instead of a password.

Related error patterns

More