pgcrypto: Cryptography and Password Hashing Extension
Last updated: 2026-11-04
pgcrypto: Cryptography and Password Hashing Extension
pgcrypto ships digest(), hmac() and gen_random_bytes() as cryptographic primitives, plus the widely used crypt() / gen_salt() pair for password hashing. crypt() supports md5, sha256, sha512 and bcrypt (bf), always with a random salt, producing a hash string ready to store. Compared with the built-in md5(), it adds the SHA-2 family, HMAC, and real password hashing.
| Kind | extension |
|---|---|
| Example Language | SQL |
📝 Syntax
CREATE EXTENSION IF NOT EXISTS pgcrypto;
SELECT crypt('secret', gen_salt('bf', 10)); -- salted password hash
SELECT encode(digest('hello', 'sha256'), 'hex'); -- SHA-256 digest
SELECT encode(gen_random_bytes(16), 'hex'); -- 16 random bytes
Returns:CREATE EXTENSION returns success on completion. crypt() returns a salted hash as text; digest() and gen_random_bytes() return bytea, normally rendered as hex via encode(); hmac() also returns bytea.
▶ Example
Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.
Output:
SELECT id, email,
left(password_hash, 7) AS hash_prefix,
length(password_hash) AS hash_len
FROM t_pgcrypto_demo
id | email | hash_prefix | hash_len
----+------------------+-------------+----------
1 | alice@example.com | $2a$08$ | 60
(1 row)
SELECT encode(gen_random_bytes(8), 'hex') AS random_bytes_hex,
length(gen_random_bytes(8)) AS random_bytes_len
random_bytes_hex | random_bytes_len
------------------+------------------
9021ec645e75435b | 8
(1 row)
SELECT encode(digest('hello', 'sha256'), 'hex') AS sha256_hex
sha256_hex
------------------------------------
2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
(1 row)
SELECT crypt('s3cret', password_hash) = password_hash AS login_ok,
crypt('wrong-pw', password_hash) = password_hash AS wrong_pw_ok
FROM t_pgcrypto_demo
login_ok | wrong_pw_ok
----------+-------------
true | false
(1 row)
Note: random_bytes_hex differs on every run (as random data should); sha256_hex is the fixed digest of 'hello'; the $2a$08$ prefix of hash_prefix denotes bcrypt with cost factor 8, and the full hash is 60 characters long.
📌 Key Points
- In browser PGlite it must be loaded on demand: import { pgcrypto } from '@electric-sql/pglite/contrib/pgcrypto', then new PGlite({ extensions: { pgcrypto } }).
- Size: pgcrypto is about 1.1 MB, by far the largest of the contrib extensions — pg_trgm (~15 KB), btree_gist (~22 KB) and uuid-ossp (~17 KB) are only a fraction of that, because pgcrypto bundles a complete cryptographic implementation (multiple digests, HMAC, bcrypt).
- crypt() generates a fresh random salt every time, so the same plaintext hashes to a different string each run. Verify with crypt(plaintext, stored_hash) = stored_hash — never compare the hash strings for equality.
- The second argument of gen_salt('bf', 8) is the cost factor, 10 by default: higher is slower and harder to brute-force. This example uses 8 to stay fast; production should use 10 to 12.
- Use digest() for sha256/sha512 checksums — better than the built-in md5() for content fingerprints — but remember a digest is not a password hash.
- gen_random_bytes() draws from the operating system's randomness source, which makes it suitable for tokens and salts; it returns bytea, so wrap it in encode() to display or transport it.
💡 Related Cases
More case studies coming soon.
📚 Related Reference
❓ FAQ
QWhy can't I compare the crypt() result with the plaintext?
ABecause crypt() salts every call, so bcrypt never produces the same string twice. The correct check re-derives the hash from the plaintext and the stored value: crypt('secret', stored_hash) = stored_hash must be true for the password to match.
QHow does pgcrypto differ from the built-in md5()?
Amd5() offers an MD5 digest and nothing else. pgcrypto adds sha224/sha256/sha384/sha512 digests, hmac(), gen_random_bytes(), and the salted password hashing pair crypt() / gen_salt(). Use sha256 for content fingerprints and crypt() for passwords — do not mix them.