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.

Kindextension
Example LanguageSQL

📝 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

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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏