Sub topic 11
Migrating PostgreSQL Roles from MD5 to SCRAM
This chapter assumes you know basic SQL and psql, PostgreSQL roles, and how to edit pg_hba.conf. The goal is to replace stored MD5 password verifiers with SCRAM-SHA-256 verifiers without locking users out.
Why MD5 requires a password reset
PostgreSQL cannot convert an existing MD5 verifier into a SCRAM verifier. Both are one-way password representations, so the server does not know the original password. Each role must receive a new password, or its owner must enter the existing password again.
The migration has two separate parts:
- Store new passwords as SCRAM verifiers.
- Change matching
pg_hba.confrules to requirescram-sha-256.
The password_encryption setting controls how PostgreSQL stores a password when it is created or changed. It does not convert passwords that are already stored.
Find roles that still use MD5
A superuser can inspect pg_authid. This catalog contains password verifiers, so limit access to trusted administrators. The query identifies roles whose stored value begins with md5.
SELECT rolname
FROM pg_authid
WHERE rolpassword LIKE 'md5%';Record the affected roles and coordinate with their application owners. A password change may require updating application secrets, connection pools, deployment variables, or service configuration.
Set new passwords as SCRAM verifiers
Set password_encryption in the session used to change passwords. Then use \password in psql. This command prompts for the password instead of placing it in SQL text, shell history, or a copied command.
SHOW password_encryption;
SET password_encryption = 'scram-sha-256';
\password app_userThe resulting verifier can be checked by a superuser:
SELECT rolname, left(rolpassword, 14) AS stored_prefix
FROM pg_authid
WHERE rolname = 'app_user';For a SCRAM verifier, stored_prefix starts with SCRAM-SHA-256$. The full verifier contains a salt and derived keys. It does not contain the plain-text password.
A script can use ALTER ROLE, but avoid putting real passwords directly in scripts or command history. Prefer a secret manager, an interactive prompt, or a controlled administrative process.
Migrate in a safe order
Keep the existing MD5 rule temporarily while changing roles. PostgreSQL can authenticate an MD5-stored role through an md5 rule. After a role receives a SCRAM verifier, the same md5 rule automatically negotiates SCRAM for that role in supported PostgreSQL clients.
After all clients and roles are ready, replace the relevant md5 records in pg_hba.conf with scram-sha-256. Remember that the first matching record wins, so an earlier broad md5 record can still control a connection.
Check the migration
Use the HBA view to find syntax errors and confirm rule order:
SELECT line_number, type, database, user_name, address, auth_method, error
FROM pg_hba_file_rules
ORDER BY line_number;Test with the same database, role, host, and TLS settings that the application uses. A successful test proves both that the selected rule is correct and that the role has a compatible verifier.
Exercise
Why can PostgreSQL not automatically convert an existing MD5 verifier to SCRAM?
Show answer
An MD5 verifier is a one-way representation and does not reveal the original password. The role must be assigned its password again so PostgreSQL can create a SCRAM verifier.