md5(text) — Compute the MD5 digest of a string
Last updated: 2026-11-04
md5(text) — Compute the MD5 digest of a string
md5() returns the MD5 digest of the input as a fixed 32-character hexadecimal string. It is handy for cache keys, fingerprinting non-sensitive content and detecting whether data changed. MD5 is no longer collision resistant, so for password storage use crypt() with gen_salt() from the pgcrypto extension instead.
| Kind | function |
|---|---|
| Example Language | SQL |
📝 Syntax
md5(string text) → text
md5(string bytea) → text
⚙️ Parameters
| string text | The string to digest; NULL is treated as an empty string. Type text. |
|---|---|
| string bytea | The binary data to digest. Type bytea. |
Returns:text — a 32-character lower-case hex digest. Both the text and bytea variants return text (as of PG 18 the bytea variant no longer 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:
id | body | md5_hash | hash_len
----+-------------+----------------------------------+----------
1 | postgresql | e4728f444b24839e3f80adf3829bcba9 | 32
2 | hello world | 5eb63bbbe01eeed093cb22bb8f5acdc3 | 32
(2 rows)
📌 Key Points
- The output is always 32 lower-case hex characters; verify it with length(md5(x)).
- md5() ships with PostgreSQL core, so no extension is required.
- MD5 is no longer collision resistant and is not reversible — use it for fingerprints and cache keys, never for passwords.
💡 Related Cases
More case studies coming soon.
📚 Related Reference
❓ FAQ
QHow does md5() differ from digest() in pgcrypto?
Amd5() is built in and limited to MD5; digest(data, 'sha256') in pgcrypto supports the stronger SHA-2 family, but it requires CREATE EXTENSION pgcrypto first.