Sub topic 13
Troubleshooting common authentication errors
This chapter assumes you can read a pg_hba.conf record, know that the first matching rule wins, and have set up SCRAM passwords as in the earlier chapters. You also need shell access to the server, or at least permission to read its log. It teaches a repeatable way to diagnose a failed connection, then walks through the errors you will see most often.
Start with the server log, not the client message
PostgreSQL deliberately gives the client a vague message. A client that is told "this role does not exist" can enumerate role names, so the server reports the same password authentication failed for a wrong password, an unknown role, and a role with no password. The precise reason goes only to the server log.
A failed attempt typically looks like this:
FATAL: password authentication failed for user "app_user"
DETAIL: Role "app_user" does not exist.
Connection matched file "/etc/postgresql/16/main/pg_hba.conf" line 95: "host all all 0.0.0.0/0 scram-sha-256"The DETAIL lines are the useful part. They give the real reason, and they name the exact pg_hba.conf line that matched. Wording and the exact detail vary a little between versions.
Find the log and watch it while you reproduce the failure:
sudo -u postgres psql -Atc "SHOW log_directory; SHOW logging_collector; SHOW data_directory;"
sudo tail -f /var/log/postgresql/postgresql-16-main.logThe log path above is the Debian and Ubuntu default. On other systems check log_directory under the data directory, or use journalctl -u postgresql. If you want connection attempts logged, enable log_connections and log_disconnections in postgresql.conf.
A triage path
Work from the cheapest check to the most specific one. The first question is always whether the client reached the server and which rule it matched.
Error 1: connection refused or timed out
psql: error: connection to server at "db.example.com" (10.0.0.5), port 5432 failed: Connection refusedThis is not an authentication error. The request never reached a rule in pg_hba.conf. Check these in order:
- The server is running and
listen_addressesincludes the interface you are connecting to. The defaultlocalhostaccepts only loopback connections. Changing it needs a restart, not a reload. - The
portsetting matches the one in your connection string. - A firewall or cloud security group allows the port. A silent drop looks like a timeout, while an active reject looks like "refused".
Error 2: no pg_hba.conf entry
FATAL: no pg_hba.conf entry for host "10.0.0.7", user "app_user", database "appdb", SSL encryptionThe server read your connection's attributes and found no rule that matched all of them. The message echoes exactly what the server saw, so compare it field by field with your rules:
- Host: the client address as the server sees it. Behind a proxy or NAT, this is the proxy's address, not the laptop's.
- SSL encryption or no encryption: a
hostsslrule does not match a plaintext connection, and ahostnosslrule does not match a TLS one. Many clients try TLS first, then fall back to plaintext, which produces two log lines. - Database and user:
alldoes not match the keywordreplication. Replication connections need their own rule.
A related message is pg_hba.conf rejects connection for host .... That means a rule did match, and its method is reject. The usual cause is a broad reject placed above the rule you meant to use.
A common source of confusion is localhost versus the socket. psql -d appdb with no host uses the Unix socket, which only local rules match. psql -h localhost uses TCP, which only host rules match. Having a rule for one and not the other produces a "no entry" error that looks inexplicable until you notice this.
Error 3: password authentication failed
This client message has several server-side causes. The DETAIL line tells them apart.
| Server log detail | Meaning | Fix |
|---|---|---|
| `Role "x" does not exist.` | The user name is wrong, often a case or typo issue. | Check `\du`. Quoted names are case-sensitive. |
| `User "x" has no password assigned.` | `rolpassword` is null. | Set one with `\password x`. |
| `Password does not match for user "x".` | Wrong password. | Re-enter it, or reset it. |
| `User "x" does not have a valid SCRAM secret.` | The rule says `scram-sha-256` but the stored value is an MD5 hash. | Set the password again under `password_encryption = 'scram-sha-256'`. |
| `User "x" has an expired password.` | `VALID UNTIL` is in the past. | Extend it with `ALTER ROLE x VALID UNTIL 'infinity'`. |
The SCRAM-secret case is the typical migration failure. The method is compatible in one direction only. A rule with md5 accepts a role whose stored verifier is SCRAM, and the server uses SCRAM for it. A rule with scram-sha-256 rejects a role whose verifier is still MD5, because the server cannot compute a SCRAM proof from an MD5 hash. Find the affected roles before you tighten the rule:
SELECT rolname
FROM pg_authid
WHERE rolpassword LIKE 'md5%';Another cause is an old client. A libpq older than version 10 cannot do SCRAM and fails with an error such as authentication method 10 not supported. Upgrade the client library or driver. Do not weaken the server rule.
Error 4: peer or ident authentication failed
FATAL: Peer authentication failed for user "app_user"With peer, the server asks the operating system for the connecting process's user name and requires it to equal the requested database role, unless a map in pg_ident.conf allows otherwise. Running psql -U app_user as the shell user alice fails. You can either run the command as that OS user (sudo -u app_user psql), connect over TCP with a password rule, or add a user map. ident failures are the same idea for TCP connections and depend on an ident service, which is why most deployments avoid it.
Error 5: role or database does not exist
FATAL: role "app_user" does not exist
FATAL: database "appdb" does not existAuthentication succeeded, or the method does not check passwords, such as peer or trust. The role or database is missing. A common trap is that psql defaults the database name to the user name, so psql -U app_user tries to open a database called app_user. Always pass -d.
Error 6: channel binding and TLS failures
When a client uses channel_binding=require, the connection fails unless the server authenticates with SCRAM over TLS. Typical messages are channel binding required, but server did not offer an authentication method that supports channel binding or channel binding is required, but SSL is not in use. The causes are:
- The matched rule uses
md5,password, ortrust, notscram-sha-256. - The connection is not using TLS, for example because the rule is
hostand the client hassslmode=disable. - The role's stored verifier is not SCRAM.
If the failure is a certificate error such as certificate verify failed under sslmode=verify-full, authentication was never reached. Check the certificate chain and that the host name in the connection string matches the certificate.
Error 7: your edit did not take effect
After editing pg_hba.conf, the server uses the new rules only after a reload. A syntax error during a reload does not stop the server. It keeps the previous rules and logs the problem, so your change is silently ignored unless you look. Always verify:
SELECT pg_reload_conf();
SELECT line_number, type, database, user_name, address, auth_method, error
FROM pg_hba_file_rules
ORDER BY line_number;A non-null error marks a line the server could not parse. Remember that pg_hba_file_rules shows the file on disk as last parsed. It does not tell you which rule a particular connection will match, so read it in order and apply first-match logic yourself, or read the Connection matched line in the log after a test connection.
A bad file at server startup is different. The server refuses to start, so keep a copy of the last working file before you edit it.
A reproducible test routine
- Reproduce the failure with an explicit command:
psql "host=db.example.com dbname=appdb user=app_user". Addsslmodeif TLS matters. - Read the server log lines produced by that one attempt.
- Note the matched line number, then confirm it in
pg_hba_file_rules. - Change one thing, reload, and retest.
Exercise
A client connects with psql -h 10.0.0.9 -U app_user -d appdb. The client sees password authentication failed for user "app_user". The server log says User "app_user" does not have a valid SCRAM secret. and the matched rule uses scram-sha-256. What is the cause, and what do you do?
Show answer
The role's stored password is still an MD5 hash, and a scram-sha-256 rule cannot verify an MD5 verifier. Confirm with SELECT rolname FROM pg_authid WHERE rolpassword LIKE 'md5%';. Make sure password_encryption is scram-sha-256, then set the password again with \password app_user. This stores a SCRAM verifier, and the login then works.
Exercise
psql -d appdb works on the server, but psql -h localhost -d appdb fails with no pg_hba.conf entry for host "127.0.0.1". Why, and what should you check?
Show answer
Without -h, psql uses the Unix socket, which matches local rules. With -h localhost it uses TCP, which matches only host rules. No host rule covers 127.0.0.1 (or ::1 if the name resolves to IPv6), so none matched. Add host rules for 127.0.0.1/32 and ::1/128, reload, and check pg_hba_file_rules for an empty error column.