crypt
PG 8.1+ (pgcrypto extension required: CREATE EXTENSION pgcrypto;)→ textHashes a password using a salt. Use gen_salt() to create the salt. Supports bf (bcrypt), md5, sha1, des algorithms.
Signature
crypt ( password text, salt text ) → textParameters
| Parameter | Type | Description |
|---|---|---|
| password | text | Plaintext password to hash |
| salt | text | Salt string from gen_salt(). Pass the stored hash when verifying. |
Examples
SELECT crypt('mysecretpassword', gen_salt('bf'));$2a$08$... (bcrypt hash)SELECT (crypt('mysecretpassword', stored_hash) = stored_hash) AS valid FROM users WHERE username = 'alice';true if password matchesINSERT INTO users (username, password_hash) VALUES ('bob', crypt('s3cr3t', gen_salt('bf', 12)));User inserted with bcrypt-hashed password (work factor 12)SELECT crypt('newpassword', gen_salt('bf', 13)) AS upgraded_hash;$2a$13$... (higher work factor for 2025+ hardware)DES-crypt and MD5-crypt password hashes are trivially brute-forced with modern hardware. Any system using these legacy algorithms for new password storage is insecure regardless of the key length.
✓ Instead: Always use `crypt(password, gen_salt('bf', 12))` (bcrypt with work factor ≥ 10). Never use `gen_salt('des')` or `gen_salt('md5')` for new passwords.
Always use `gen_salt('bf', 10)` (or higher) for password hashing. Bcrypt is intentionally slow, making brute-force attacks costly. The work factor doubles computation time per increment.
-- Store:
INSERT INTO users (username, password_hash) VALUES ($1, crypt($2, gen_salt('bf', 12)));
-- Verify:
SELECT crypt($password, password_hash) = password_hash AS valid FROM users WHERE username = $1;Bcrypt password storage and verification