Sub topic 11
Migrating roles from MD5 to SCRAM
This chapter assumes you can read pg_hba.conf records, know that first match wins, and have used ALTER ROLE and psql. You should also know that password_encryption controls how new passwords are hashed. It covers moving existing roles from MD5 password hashes to SCRAM-SHA-256 verifiers without locking out clients.
Why the migration needs a plan
MD5 password authentication is weak. The stored hash is effectively the credential: anyone who obtains it can log in, because the server only checks it against a salt it sends. The hash is also fast to attack offline. SCRAM-SHA-256 fixes both problems. Recent PostgreSQL releases default to scram-sha-256 for new passwords, and PostgreSQL 18 formally deprecates MD5 authentication.
The difficulty is that a stored hash cannot be converted. An MD5 hash does not contain enough information to produce a SCRAM verifier. Each role needs its password set again, which means someone must know the plain-text password. The password can be the same as before. The server generates a fresh salt and a new verifier either way.
Two things must move together:
- The stored verifier in
pg_authid, which depends onpassword_encryptionat the moment the password is set. - The authentication method in
pg_hba.conf, eithermd5orscram-sha-256.
How rules and verifiers interact
The md5 method in pg_hba.conf is lenient. If the role's stored password is a SCRAM verifier, the server runs SCRAM instead. The scram-sha-256 method is strict. If the role still has an MD5 hash, authentication fails.
| `pg_hba.conf` method | Role has MD5 hash | Role has SCRAM verifier |
|---|---|---|
| `md5` | MD5 exchange | SCRAM exchange |
| `scram-sha-256` | Fails | SCRAM exchange |
This table sets the migration order. Change the roles first, then tighten the rule. Reversing the order locks out every role you have not converted yet.
Clients can break before the rule changes
Because md5 rules accept SCRAM verifiers, the first client failures appear when you reset a role's password, not when you edit pg_hba.conf. A client whose library does not support SCRAM cannot log in as that role afterward. Libpq added SCRAM support in version 10. Old releases of other drivers, such as PostgreSQL JDBC before 42.2.0, lack it. Poolers and proxies also need support, and PgBouncer needs version 1.11 or later with its own authentication configured for SCRAM.
An old libpq reports an error like this:
psql: error: authentication method 10 not supportedInventory your clients before you start. Upgrade drivers first, then convert roles one at a time, starting with the least critical.
Step 1: Audit the roles
Reading pg_authid needs superuser rights. This query groups login roles by the type of password they hold:
SELECT
CASE
WHEN rolpassword IS NULL THEN 'none'
WHEN rolpassword LIKE 'md5%' THEN 'md5'
WHEN rolpassword LIKE 'SCRAM-SHA-256$%' THEN 'scram-sha-256'
ELSE 'other'
END AS verifier,
count(*) AS roles
FROM pg_authid
WHERE rolcanlogin
GROUP BY 1
ORDER BY 1;Roles with no password are not affected by this migration. They log in through peer, cert, or another method that needs no stored secret. List the roles that need work:
SELECT rolname
FROM pg_authid
WHERE rolpassword LIKE 'md5%'
ORDER BY rolname;Then look at which rules your clients actually use:
SELECT line_number, type, database, user_name, address, auth_method
FROM pg_hba_file_rules
WHERE auth_method IN ('md5', 'password')
ORDER BY line_number;A password rule sends the password in clear text and accepts either stored format. Plan to replace those too.
Step 2: Make new passwords SCRAM
Setting password_encryption does not rehash anything already stored. It only affects passwords set afterward. To check the current value and change it for one session:
SHOW password_encryption;
SET password_encryption = 'scram-sha-256';To make it the server default, change it and reload:
ALTER SYSTEM SET password_encryption = 'scram-sha-256';
SELECT pg_reload_conf();A session-level SET is enough for a one-off migration, but the server-wide setting protects you from new roles being created with MD5 later. A role's own ALTER ROLE ... SET password_encryption or a per-database setting can override the default, so check pg_db_role_setting if a new role unexpectedly gets an MD5 hash.
Step 3: Set the passwords again
In psql, \password prompts for the new password and sends it already hashed with the current password_encryption setting. The plain text does not reach the server logs or your shell history.
SET password_encryption = 'scram-sha-256';
\password app_userAvoid ALTER ROLE app_user PASSWORD 'secret' unless statement logging is off. The plain-text password in the statement can be written to the server log.
Confirm that the stored value changed:
SELECT rolname, left(rolpassword, 14) AS stored_prefix
FROM pg_authid
WHERE rolname = 'app_user';The prefix should be SCRAM-SHA-256$. Use the same command for each role in your audit list.
Dumps keep whatever hash they contain. If you restore a pg_dumpall --roles-only file taken before the migration, MD5 hashes come back and you must repeat the process.
Step 4: Stage the rule change
Do not swap md5 for scram-sha-256 across the board in one edit. Add a stricter rule for converted roles above the general one. First match wins, so converted roles use the strict rule and the rest keep using the lenient one:
# TYPE DATABASE USER ADDRESS METHOD
hostssl appdb app_user 10.0.0.0/24 scram-sha-256
host all all 10.0.0.0/24 md5Reload, then check the parsed rules and their order:
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 value means the line did not parse and is ignored. If you edit the rules incorrectly, a client may match a different line than you expected, so read the order column by column.
Raising log_connections to on helps with the rollout. In PostgreSQL 14 and later the server logs a line like connection authenticated: identity="app_user" method=scram-sha-256 (/etc/postgresql/pg_hba.conf:84). It tells you which rule and method each login used, so you can see when the last client moves off md5.
Step 5: Finish and clean up
When the audit query shows no md5 hashes, replace the remaining md5 rules with scram-sha-256, reload, and test each application. The rollout looks like this:
If a client fails after a reset, you can restore the old MD5 hash only if you saved it. Before starting, record the existing hash for each role, and store that record securely because the hash is a usable credential. The usual fix is to upgrade the client instead.
Common failures
FATAL: password authentication failed for user "app_user"right after you tighten a rule. The server log showsDETAIL: User "app_user" does not have a valid SCRAM secret.The role still has an MD5 hash. Reset its password.authentication method 10 not supportedmeans the client library is too old for SCRAM. Upgrade it.- Pooler logins fail but direct logins work. The pooler keeps its own copy of the credentials, such as a PgBouncer
userlist.txt, or runs its own lookup query. Update that copy as well. - New roles still get MD5 hashes. A role or database level
password_encryptionsetting overrides the server value. Checkpg_db_role_setting.
Summary of the order
- Audit the stored hashes, the rules, and the client libraries.
- Upgrade clients and poolers that cannot do SCRAM.
- Set
password_encryption = 'scram-sha-256'server-wide. - Reset each role's password, using
\password. - Stage stricter rules ahead of the lenient ones, reload, and read
pg_hba_file_rules. - When no MD5 hashes remain, replace the
md5rules withscram-sha-256.
Exercise
A role report_user still has an MD5 hash. Your pghba.conf has a single rule host all all 10.0.0.0/24 scram-sha-256. You then set `passwordencryption = 'scram-sha-256' server-wide and reload. Can report_user` log in? What must you do?
Show answer
No. Changing passwordencryption does not rehash stored passwords, so the role still has an MD5 hash, and the scram-sha-256 rule rejects it. The server log says the user does not have a valid SCRAM secret. Reset the password with \password reportuser while passwordencryption is scram-sha-256, then confirm that rolpassword starts with SCRAM-SHA-256$ in pgauthid.
Exercise
You reset the password of batch_user to a SCRAM verifier while the matching rule is still md5. The nightly job, which uses a 2016 JDBC driver, now fails. Why did the failure appear at the password reset and not when the rule changed? What are two ways to fix it?
Show answer
An md5 rule runs SCRAM when the role has a SCRAM verifier, so the server demanded a SCRAM exchange as soon as the password changed, and the old driver cannot do one. Fix it by upgrading the driver to a version with SCRAM support, or by restoring the saved MD5 hash for the role until the driver can be upgraded.