Sub topic 7
How passwords are stored: `password_encryption` and `pg_authid`
Assume you know basic SQL, psql, PostgreSQL roles, and configuration files. This chapter focuses on how PostgreSQL stores role passwords. The pg_hba.conf method decides whether a password is requested, while password_encryption decides how a newly set password is stored.
Passwords are stored as verifiers
PostgreSQL does not need to store a role's plain-text password. Instead, it stores a password verifier. A verifier contains enough information for a password authentication exchange, but it is not the original password.
The password_encryption setting controls the format used when PostgreSQL creates or replaces a password verifier. It does not convert existing verifiers automatically.
PostgreSQL supports these important formats:
| Format | Stored prefix | Main consideration |
|---|---|---|
| MD5 | `md5` | Older format with weaker protection |
| SCRAM-SHA-256 | `SCRAM-SHA-256$` | Current recommended format |
The setting can be checked with SHOW:
SHOW password_encryption;A value of scram-sha-256 is preferred for new passwords.
Inspecting stored verifiers
The pg_authid system catalog contains role authentication data. Its rolpassword column contains the verifier, not the plain-text password. Access to pg_authid is restricted because the verifier is still sensitive. A superuser can inspect it, but ordinary users normally cannot.
SELECT rolname, left(rolpassword, 14) AS stored_prefix
FROM pg_authid
WHERE rolname = 'app_user';For a SCRAM verifier, stored_prefix begins with SCRAM-SHA-256$. For an MD5 verifier, it begins with md5. A role with no password has a null rolpassword.
Do not copy complete verifier values into tickets, logs, or source control. An attacker who obtains a verifier may be able to attempt offline password guesses.
Setting a password with SCRAM
Set the format before setting or resetting the role password. The setting affects the current session, so it does not change the server-wide default.
SET password_encryption = 'scram-sha-256';
-- In psql, enter the password interactively.
\password app_user
SELECT rolname, left(rolpassword, 14) AS stored_prefix
FROM pg_authid
WHERE rolname = 'app_user';The \password command is a psql command, not SQL. It prompts for the password instead of placing the plain text in a SQL statement, shell history, or a script.
A SQL statement such as ALTER ROLE app_user PASSWORD '...' can work, but the plain text may be exposed through command history, logs, process inspection, or auditing. Use an interactive prompt or a secret-management system when possible.
What SCRAM stores
A SCRAM verifier includes a salt, an iteration count, and derived keys. The client proves knowledge of the password during the authentication exchange. The password itself is not sent to the server.
Exercise
Exercise
A role's pgauthid.rolpassword begins with md5, but passwordencryption is now scram-sha-256. What must you do to migrate the role?
Show answer
Reset the role's password in a session where password_encryption is scram-sha-256, preferably with \password. The existing MD5 verifier is not converted automatically.