Agent Wranglers

DAT-66Recommended

Log in to the database with SCRAM instead of MD5

Database logins that use a password use a challenge method such as SCRAM-SHA-256. The server refuses MD5 and plain-text password logins.

How to do it

In PostgreSQL, set password_encryption = scram-sha-256 and then set each password again, so the stored hash changes. Use scram-sha-256 in every pg_hba.conf line, never md5, password or trust. PostgreSQL 18 deprecates MD5 passwords and plans to remove them in a future major release. Clients can insist on SCRAM with require_auth=scram-sha-256 in the connection string. Set a pooler such as PgBouncer to SCRAM too, as in Pool database connections. Encrypt the connection as well, as Force encryption on every managed service says. Give that login only the rights in Give the app a least-privilege database user.

Examples

SQL example

ALTER SYSTEM SET password_encryption = 'scram-sha-256';
SELECT pg_reload_conf();
ALTER ROLE shop_app PASSWORD 'new-password-from-the-secrets-store';
SELECT rolname FROM pg_authid WHERE rolpassword LIKE 'md5%';   -- must return no rows

Plain text example

# pg_hba.conf
hostssl  shop  shop_app  10.0.0.0/24  scram-sha-256

Shell example

psql "host=db.example.com dbname=shop user=shop_app sslmode=verify-full require_auth=scram-sha-256" -c 'select 1'
grep -nE '\s(md5|password|trust)\s*$' /etc/postgresql/*/main/pg_hba.conf   # must print nothing

How to check

The query on stored hashes returns no role with an MD5 password. No line in pg_hba.conf uses md5, password or trust for a network address. A connection with require_auth=scram-sha-256 succeeds for each app user.

Sources

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/data/operations/database-scram-auth">Agent Wranglers data playbook</a>. In code you write, add a comment next to it with the same link.