Sub topic 15
Hardening Checklist for PostgreSQL Authentication
Assume you know basic SQL, PostgreSQL roles, psql, Linux configuration files, and CIDR notation. This checklist brings those pieces together. Hardening means reducing the ways an unauthorized client can connect, limiting what an authenticated role can do, and making configuration mistakes visible.
Start with a narrow connection policy
Review every pg_hba.conf rule from top to bottom. PostgreSQL uses the first rule that matches a connection, so a broad rule placed early can bypass a stricter rule later.
A useful baseline is:
# TYPE DATABASE USER ADDRESS METHOD
local all postgres peer
local all all scram-sha-256
hostssl appdb app_user 10.0.0.0/24 scram-sha-256
host all all 0.0.0.0/0 rejectThe final reject rule makes the intended boundary explicit. It does not replace firewall rules. The database server should also accept traffic only from networks that need it.
Use the narrowest values possible:
- Name a database instead of using
allwhen the role needs one database. - Name an application role instead of using
allfor users. - Use a specific host or small CIDR range instead of a whole network.
- Put special-case rules before general rules.
- Avoid
trustexcept for carefully controlled, temporary local administration.
Prefer strong authentication
For password authentication, use scram-sha-256. SCRAM proves knowledge of the password without sending the password itself over the connection. It also uses a salted verifier, which is safer than an old MD5 verifier.
Make sure role privileges are as narrow as the connection rules:
| Area | Safer choice |
|---|---|
| Application role | `LOGIN`, no `SUPERUSER`, no `CREATEDB` |
| Password storage | SCRAM-SHA-256 |
| Remote access | `hostssl` with a limited CIDR |
| Local administrator | `peer` over a Unix socket |
| Unknown connections | Explicit `reject` |
A role that can connect is not automatically authorized to read every table. Keep authentication and authorization separate. Grant only the database, schema, table, and sequence privileges the application requires.
ALTER ROLE app_user NOSUPERUSER NOCREATEDB NOCREATEROLE NOREPLICATION;
GRANT CONNECT ON DATABASE appdb TO app_user;Require TLS for remote clients
Use hostssl when credentials or data cross a network. Configure the server certificate and client trust store, then connect with certificate verification:
psql "host=db.example.com dbname=appdb user=app_user sslmode=verify-full channel_binding=require"verify-full checks both the certificate chain and the server name. channel_binding=require asks the client and server to bind SCRAM authentication to the TLS channel, which helps prevent a man-in-the-middle attack. Confirm that your client library supports this setting before enforcing it.
Validate changes and monitor failures
After editing the file, reload rather than restart PostgreSQL:
SELECT pg_reload_conf();
SELECT line_number, type, database, user_name, address, auth_method, error
FROM pg_hba_file_rules
ORDER BY line_number;Fix every nonempty error value. Test both an expected successful connection and an expected denial. Keep a versioned backup of configuration changes, but never commit passwords or private keys.
Exercise
Why should an explicit reject rule appear after the allowed rules rather than before them?
Show answer
PostgreSQL stops at the first matching rule. A reject rule near the top would also match allowed clients and deny them before they reach the intended authentication rules.