Sub topic 9
Setting up SCRAM for roles and rules
This chapter configures SCRAM-SHA-256 end to end. You will set how passwords are hashed, give a role a SCRAM password, and write pg_hba.conf rules that require it. You will then confirm that the rules and the stored hashes agree.
It assumes you can edit files on the database server, use psql, and read a pg_hba.conf record. You should also know that rules are checked top to bottom and the first match wins. Earlier chapters covered the SCRAM exchange itself, so this one is about configuration.
Two settings must agree
Whether SCRAM works depends on two independent things:
- The method named in the matching
pg_hba.confrule. This is the server's demand. - The verifier stored for the role in
pg_authid.rolpassword. This is what the server can check against.
A verifier is the stored, salted value derived from the password, not the password. Its format depends on how it was created. The server cannot convert an MD5 hash into a SCRAM verifier, because the original password is not recoverable from either one.
| Rule method | Stored verifier | Result |
|---|---|---|
| `scram-sha-256` | SCRAM | Works. |
| `scram-sha-256` | MD5 | Fails. The log says the user does not have a valid SCRAM secret. |
| `md5` | SCRAM | Works. The server performs a SCRAM exchange instead. |
| `md5` | MD5 | Works with the weaker MD5 exchange. |
| `scram-sha-256` | none (NULL password) | Fails. |
The md5 row with a SCRAM verifier is what makes migration safe. You can store SCRAM verifiers while the md5 rules are still in place, and clients keep working. The second row is the trap. If you change a rule to scram-sha-256 before the role has a new password, that role is locked out.
Step 1: choose the hashing scheme
The password_encryption setting controls how the server hashes a plain-text password when a role is created or changed. Since PostgreSQL 14 the default is scram-sha-256. On older servers, or on a cluster whose configuration was carried forward, it may still be md5.
Check the current value, then set it for your session. The same block sets the role's password and verifies the result.
SHOW password_encryption;
SET password_encryption = 'scram-sha-256';
-- In psql, \password prompts for the password and sends it already hashed,
-- so the plain text does not end up in server logs or shell history.
\password app_user
SELECT rolname, left(rolpassword, 14) AS stored_prefix
FROM pg_authid
WHERE rolname = 'app_user';A correct result shows SCRAM-SHA-256$ in the stored_prefix column. An MD5 hash starts with md5 instead.
To make the choice permanent, set password_encryption = 'scram-sha-256' in postgresql.conf and reload. A session-level SET only affects passwords set in that session.
Changing password_encryption does not rehash existing passwords. It only affects passwords set afterwards. Every role that should move to SCRAM needs its password set again.
Step 2: set role passwords safely
There are several ways to set a password, and they differ in what they leak.
\password role_nameinpsqlis the preferred way. The client hashes the password before sending it, so the server never sees the plain text in a statement and nothing is written to the statement log.ALTER ROLE app_user PASSWORD 'secret'works, and the server hashes it withpassword_encryption. The plain text appears in the statement text, so it can reach server logs (withlog_statementorlog_min_duration_statement),pg_stat_activityand shell history.- You can also pass a ready-made verifier string. If it starts with
SCRAM-SHA-256$, the server stores it unchanged. This is how tools that generate verifiers client-side load them.
The pg_authid catalog is readable only by superusers. Use it for audits. The pg_roles view hides the password column and is the right tool for everything else.
Never put a real password in a migration script that is kept in version control. Pass it from a secrets store, and prefer a client that hashes before sending.
Step 3: write rules that require SCRAM
Now write the rules. The file below uses peer for the local superuser (the operating system user name must match), SCRAM for every other path, and an explicit reject at the end.
# TYPE DATABASE USER ADDRESS METHOD
local all postgres peer
local all all scram-sha-256
host all all 127.0.0.1/32 scram-sha-256
host all all ::1/128 scram-sha-256
hostssl appdb app_user 10.0.0.0/24 scram-sha-256
host all all 0.0.0.0/0 rejectPoints to notice:
- The
postgresline comes before the generallocalline. With first match wins, reversing them would make the superuser go through a password prompt. - The
hostsslline only accepts TLS connections. A non-TLS connection from10.0.0.0/24skips that line. It then falls through to the last line and is rejected. This is intended. - The final
rejectis redundant, because a connection that matches no rule is refused anyway. Writing it makes the intent visible and gives you a place to hang a comment. - The
0.0.0.0/0line covers IPv4 only. If the server listens on IPv6 addresses, add a::/0line too, or rely on the implicit refusal.
The scram-sha-256 method works in local, host, hostssl and hostgssenc records. Only peer and ident-style methods are tied to local sockets or ident lookups.
Avoid the method named password. It asks the client for the password in clear text, protected only if TLS is in use. It accepts SCRAM verifiers, but it does not use the SCRAM exchange, so it throws away the main benefit.
Step 4: apply and verify
Editing pg_hba.conf changes nothing until the server rereads it. A reload is enough, and no restart is needed. Then read back how the server parsed the file.
SELECT pg_reload_conf();
SELECT line_number, type, database, user_name, address, auth_method, error
FROM pg_hba_file_rules
ORDER BY line_number;pg_hba_file_rules reflects the file on disk, not the rules the server is currently running. A line with a syntax error appears with text in the error column, and the server keeps its previous rules in effect. Always check that error is empty for every row before you close your session. If you lock yourself out, you will need another way in, such as the local ... peer line for the superuser.
A cautious procedure on a remote server:
- Keep one working
psqlsession open. - Edit the file and run the two statements above in that session.
- Open a second connection as the affected role from a real client host.
- Close the first session only when the second one works.
Check for roles that would be locked out
Before changing any md5 rule to scram-sha-256, find roles whose stored value is still an MD5 hash.
SELECT rolname
FROM pg_authid
WHERE rolpassword LIKE 'md5%';Every role this returns needs a new password set while password_encryption is scram-sha-256. The full migration is covered in a later chapter. For this chapter, the point is that the query should return no rows before you tighten the rules.
Common problems when setting up
- A role keeps authenticating with MD5 after you changed
password_encryption. The password was not set again, so the old hash is still stored. FATAL: password authentication failedright after you changed a rule. Check the server log. If it says the user "does not have a valid SCRAM secret", the stored hash is MD5.- An old client library cannot connect. SCRAM needs libpq 10 or later, or a driver that implements it. Upgrade the driver instead of weakening the rule.
- A connection pooler sits in front of the server. The pooler holds its own authentication settings and credentials, and the
pg_hba.confrule applies only to the pooler-to-server hop. Configure both sides for SCRAM. - Your change had no effect. You may have edited a different file. Run
SHOW hba_file;to see which file the server reads.
Exercise
A cluster has password_encryption = 'md5'. You change it to scram-sha-256 in postgresql.conf, reload, and change the rule for app_user from md5 to scram-sha-256. app_user can no longer log in. Why, and how do you fix it without editing pg_hba.conf again?
Show answer
Changing password_encryption only affects passwords set afterwards, so app_user still has an MD5 hash in pg_authid. The scram-sha-256 rule needs a SCRAM verifier, and the server cannot convert the old hash. Connect as a superuser, run \password app_user to set the password again (it is now hashed as SCRAM), and check that left(rolpassword, 14) shows SCRAM-SHA-256$. No change to pg_hba.conf or a reload is needed for the password change.
Exercise
You add a line host all all 10.0.0.0/24 reject above an existing hostssl appdb app_user 10.0.0.0/24 scram-sha-256 line, then reload. pg_hba_file_rules shows no errors. Will app_user still connect from 10.0.0.5 over TLS?
Show answer
No. A connection matches the first line whose type, database, user and address fit it, and the host line matches TLS connections as well. The reject line comes first, so the hostssl line is never reached. Moving the hostssl line above the reject line fixes it. This shows why a view with no errors only proves the syntax is valid and does not prove the order is right.