Sub topic 13
Troubleshooting Common PostgreSQL Authentication Errors
Assume you know basic SQL and psql, understand PostgreSQL roles and databases, and can edit configuration files on the server. This chapter uses those skills to diagnose failures in pg_hba.conf, password authentication, and TLS connections.
Read the Error Before Changing Configuration
PostgreSQL usually identifies the failing stage:
no pg_hba.conf entrymeans no rule matched the connection's type, database, user, or address.password authentication failedmeans a matching password rule was found, but the supplied password did not verify.SSL connection is requiredorno encryptionoften means the connection does not satisfy ahostsslorhostnosslrule.- A configuration reload error means PostgreSQL could not parse one or more records.
Record the connection details before investigating:
- The database name and role name.
- Whether the client uses a Unix socket or TCP.
- The client IP address.
- Whether TLS is enabled.
- The exact error message.
A socket connection matches local records. A TCP connection matches host, hostssl, or hostnossl records. The same client can therefore match different rules depending on its connection options.
Check the Matching Rule
PostgreSQL checks pg_hba.conf from top to bottom. The first record that matches all connection fields is used. PostgreSQL does not continue searching after an authentication failure.
For example, this rule blocks a connection before a later password rule can help:
host all all 10.0.0.0/24 reject
host appdb app_user 10.0.0.0/24 scram-sha-256Move the specific rule above the broader rejection rule if the connection should be allowed.
Use the parsed rule view to inspect order and syntax:
SELECT line_number, type, database, user_name, address,
auth_method, error
FROM pg_hba_file_rules
ORDER BY line_number;An error value identifies a record PostgreSQL could not parse. Fix that record, then reload the configuration.
Fix Missing Entries
For no pg_hba.conf entry, compare the connection with every field in the candidate record:
| Connection property | Example value | HBA field |
|---|---|---|
| Transport | TCP over TLS | `type` |
| Database | `appdb` | `database` |
| Role | `app_user` | `user` |
| Client address | `10.0.0.42` | `address` |
| Authentication | SCRAM | `method` |
A rule for 127.0.0.1/32 does not match a client connecting from 10.0.0.42. A hostssl rule does not match a non-TLS connection. Confirm the actual client address from the server log rather than guessing.
Fix Password Failures
A matching SCRAM rule does not prove that the role has a usable SCRAM verifier. Check the role as a superuser:
SELECT rolname, left(rolpassword, 14) AS stored_prefix
FROM pg_authid
WHERE rolname = 'app_user';A prefix of SCRAM-SHA-256$ indicates a SCRAM verifier. A prefix of md5 indicates an old MD5 verifier. Reset the password after selecting SCRAM storage:
SET password_encryption = 'scram-sha-256';
\password app_userAlso check for simple causes: the client may be using a different role, database, host, or password file than expected. Avoid placing passwords in shell history or connection strings when possible.
Reload and Test Safely
After editing the file, ask PostgreSQL to reload it:
SELECT pg_reload_conf();Then query pg_hba_file_rules and test the exact connection again. A successful reload does not mean the intended rule will match. Verify both the parsed configuration and the first matching record.
Exercise
A client at 10.0.0.42 receives “no pghba.conf entry” while connecting to appdb as appuser over TCP without TLS. A rule says hostssl appdb app_user 10.0.0.0/24 scram-sha-256. What is wrong?
Show answer
The rule requires TLS because its type is hostssl. Add a suitable non-TLS host rule only if that connection is acceptable, or configure the client to use TLS.