Sub topic 7
How passwords are stored: password_encryption and pg_authid
This chapter assumes you can run SQL in psql, know what a role is, and have read the earlier chapters on pg_hba.conf rules and authentication methods. Here you will see what PostgreSQL keeps on disk when you give a role a password, and which setting controls its form. The next chapters build on this: the SCRAM exchange, the rule setup and the MD5 migration all depend on the stored value.
What the server actually stores
PostgreSQL never stores a password in a form it can read back. When you set a role's password, the server turns it into a verifier and writes that to the system catalog pg_authid, in the column rolpassword. At login the server compares the client's proof against the verifier. It doesn't need the original password.
Three facts about this catalog matter in practice:
pg_authidis readable only by superusers by default. Thepg_rolesview shows the same roles but displays********in the password column.- A role with no password has
NULLinrolpassword. Password-based methods cannot succeed for that role, butpeer,certand other non-password methods can. - The verifier is stored per role, not per rule. The
pg_hba.confmethod you choose later has to be compatible with what is stored here.
The password_encryption setting
The password_encryption parameter decides which kind of verifier the server builds when it receives a plain-text password. These are the values you should know:
| Value | Verifier produced | Notes |
|---|---|---|
| `scram-sha-256` | `SCRAM-SHA-256$...` | Default since PostgreSQL 14. Use this. |
| `md5` | `md5` followed by 32 hex characters | Legacy. Deprecated in recent releases. |
Older versions also accepted on, true and similar spellings as aliases for md5. Don't rely on them.
Check the current value:
SHOW password_encryption;The setting is applied only at the moment a password is set. Changing it does not convert any existing verifier. A role whose password was set while the server used md5 keeps its MD5 hash until someone sets the password again. This is why the migration chapter has to reset every password.
Any user can change the setting for their own session, so you can choose the scheme explicitly rather than depend on the server default.
SET password_encryption = 'scram-sha-256';For a permanent change, set it in postgresql.conf or with ALTER SYSTEM, then reload the configuration. Existing sessions keep the value they started with unless they SET it themselves.
Reading the stored value
Superusers can look at the verifier directly. The left() call below shows only the prefix, which is enough to tell the scheme apart without printing the whole hash.
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';If the scheme is SCRAM, stored_prefix is SCRAM-SHA-256$. An MD5 value starts with md5.
Anatomy of a SCRAM verifier
A SCRAM verifier is one string with four parts:
SCRAM-SHA-256$4096:<salt>$<StoredKey>:<ServerKey>SCRAM-SHA-256names the mechanism.4096is the iteration count of the key-derivation function. Higher values make offline guessing slower. PostgreSQL 16 and later let you change it with thescram_iterationsparameter. Earlier versions use 4096.- The salt is random and unique to each password. Two roles with the same password get different verifiers.
StoredKeyandServerKeyare derived from the password, salt and iteration count. The next chapter shows how the client and server use them to prove knowledge of the password without sending it.
Neither key is the password, and neither can be used to recover it directly. Still, a stolen verifier is not harmless: it lets an attacker try guesses offline, and the iteration count is the main brake on that. The ServerKey also lets someone who has it impersonate the server to a client. Treat the verifier as sensitive data, and keep access to pg_authid and to backups tight.
Anatomy of an MD5 hash
The legacy format is the string md5 followed by the MD5 digest of the password concatenated with the role name. There is no random salt, and the role name acts as the only salt. Two consequences follow:
- Two roles with the same name on different servers, and the same password, produce the same hash.
- The stored hash is directly usable to log in under the
md5method. An attacker who reads the hash frompg_authidor a backup can authenticate without cracking it.
That second point is the main reason to leave MD5. SCRAM does not have this weakness, because the proof depends on a fresh nonce for every login.
Setting passwords safely
There are several ways to set a password, and they differ in what leaks.
\password role_namein psql. The client asks for the password twice, hashes it locally with the server's currentpassword_encryptionsetting, and sends only the verifier. The plain text never reaches the server, its logs or your shell history. Prefer this.ALTER ROLE app_user PASSWORD 'secret';. The plain text travels in the SQL statement. Iflog_statementis set toddlorall, or if the statement fails andlog_min_error_statementrecords it, the password can appear in the server log. Without TLS it also crosses the network readable.- Supplying a precomputed verifier as the value. If the string already has the form of an MD5 hash or a SCRAM verifier, the server stores it unchanged. This is how tools restore roles from dumps, and how you can provision a role without ever sending a password.
Also useful:
-- Remove the password so password-based methods can no longer succeed.
ALTER ROLE batch_user PASSWORD NULL;Use this for roles that authenticate only by peer or cert, so that a forgotten password cannot be used on a network rule that someone adds later.
How the stored scheme interacts with pg_hba.conf
The verifier's type limits which methods can work:
| Stored verifier | `scram-sha-256` rule | `md5` rule | `password` rule |
|---|---|---|---|
| SCRAM | Works | Works, and the server uses SCRAM | Works, but sends the password in clear text |
| MD5 | Fails | Works | Works, but sends the password in clear text |
| `NULL` | Fails | Fails | Fails |
The key row is MD5 under a scram-sha-256 rule. The server cannot run a SCRAM exchange against an MD5 hash, so the login fails with a password authentication error even though the password is correct. You hit this when you change the rule before the passwords are reset. The reverse order is safe, because an md5 rule accepts SCRAM verifiers. This asymmetry gives the migration its order: reset passwords first, then tighten the rules.
Finding roles that still use MD5
This query lists roles whose verifier is an MD5 hash. Run it as a superuser.
SELECT rolname
FROM pg_authid
WHERE rolpassword LIKE 'md5%';Every role it returns needs a new password before you replace md5 rules with scram-sha-256. You cannot convert the hash. You do not know the original password, and the server cannot derive a SCRAM verifier from an MD5 digest.
Common mistakes
- Changing
password_encryptionand expecting old passwords to change. Only passwords set afterward are affected. - Setting the parameter in your session, then having an application or a different tool set the password. The encryption used is the one in effect for the session that sets it.
\passworduses the server's value for the connection you are on. - Checking
pg_rolesfor the hash. It masks the value. Usepg_authidas a superuser. - Putting plain-text passwords in
ALTER ROLEstatements in scripts that are logged or kept in version control.
Exercise
A DBA changes password_encryption from md5 to scram-sha-256 in postgresql.conf and reloads. She then edits a pghba.conf rule for `appuser from md5 to scram-sha-256. The password for app_user` was set two years ago. Logins now fail, even with the correct password. Explain why, and give the fix and a query that finds other roles with the same problem.
Show answer
The parameter only affects passwords set after the change, so app_user still has an MD5 hash in pg_authid.rolpassword. A scram-sha-256 rule needs a SCRAM verifier, and the server cannot build a SCRAM exchange from an MD5 hash, so authentication fails. The fix is to set the password again with password_encryption = 'scram-sha-256' in effect, for example with \password app_user in psql. To find the other affected roles, run SELECT rolname FROM pg_authid WHERE rolpassword LIKE 'md5%'; as a superuser. Until those roles are reset, the safe interim step is to keep an md5 rule for them, because an md5 rule also accepts SCRAM verifiers.
Exercise
You run SELECT left(rolpassword, 14) FROM pg_authid WHERE rolname = 'report_user'; and get NULL. What does it mean, and which pg_hba.conf methods can still work for this role?
Show answer
The role has no password, because rolpassword is NULL. Password-based methods (scram-sha-256, md5, password) cannot succeed for it. Methods that do not use a stored password, such as peer or cert, can still work, subject to the matching rule.