Sub topic 8
How SCRAM-SHA-256 Works
This chapter assumes that you know basic SQL and psql, understand PostgreSQL roles, and can read a pg_hba.conf rule. It focuses on the exchange that happens after PostgreSQL selects a scram-sha-256 authentication rule.
SCRAM-SHA-256 is a password-authentication protocol. It proves that the client knows a role's password without sending the password itself across the network. Authentication answers “Who are you?” Authorization happens afterward, when PostgreSQL checks what that authenticated role may do.
The working model
PostgreSQL does not need to store the original password. Instead, it stores a SCRAM verifier containing:
- A salt, which makes identical passwords produce different results.
- An iteration count, which makes guessing more expensive.
- A stored key and a server key derived from the password.
A verifier commonly has this shape:
SCRAM-SHA-256$4096:salt$stored-key:server-keyThe exact salt and key values are different for each password. A stolen verifier does not immediately reveal the password, but an attacker can still try password guesses against it. Strong, unique passwords remain important.
The password_encryption setting controls the format PostgreSQL uses when it creates or changes a role password. It does not by itself force every connection to use SCRAM. The matching pg_hba.conf rule must also specify scram-sha-256.
The SCRAM exchange
The client and server exchange several messages:
- The client sends a username and a randomly generated client nonce.
- The server returns its own nonce, the salt, and the iteration count.
- The client derives a salted password by applying PBKDF2-HMAC-SHA-256 repeatedly.
- The client creates a proof from the derived key and sends that proof to the server.
- The server verifies the proof against its stored verifier.
- The server returns a server signature. The client verifies this signature to confirm that it is talking to a server that knows the verifier.
The password is never included in these messages. The proof is tied to the complete exchange, including the nonces and the selected username. Replaying an old proof should therefore fail in a new session.
What the proof contains
SCRAM derives a SaltedPassword from the password, salt, and iteration count. It then derives two keys:
ClientKey, used to construct the client's proof.ServerKey, used to construct the server's signature.
The client combines the exchange transcript into an authentication message. It hashes and signs that message with the derived keys. The client proof is designed so that the server can verify it without receiving the ClientKey directly.
This design prevents the server from needing the plaintext password. It also prevents a captured client proof from being reused with different nonces.
| Property | SCRAM-SHA-256 behavior |
|---|---|
| Password sent over the network | No |
| Stored database value | Salted verifier |
| Replay resistance | Uses fresh nonces and a transcript |
| Protection from eavesdropping | Stronger than plaintext exchange, but TLS is still recommended |
| Protection after verifier theft | Limited by password strength and guessing cost |
Important limits and gotchas
SCRAM protects the password exchange, but it does not encrypt all database traffic. Queries and results can still be observed or altered if the connection does not use TLS. Use TLS for remote connections, and consider channel binding when both the client library and server configuration support it.
A client must support SCRAM-SHA-256 to use a SCRAM rule. Older PostgreSQL drivers may fail with errors such as “SCRAM authentication requires libpq version 10 or above.” Upgrading the driver is safer than weakening the server rule.
Changing pg_hba.conf to scram-sha-256 does not convert existing MD5 verifiers. Each affected role must receive a new password so PostgreSQL can store a SCRAM verifier.
Exercise
Why does PostgreSQL need a new password assignment when migrating a role from an MD5 verifier to SCRAM-SHA-256?
Show answer
An MD5 verifier cannot be converted into a SCRAM verifier because the original password is not recoverable from the MD5 value. Assigning the password again lets PostgreSQL derive and store the SCRAM verifier.