PostgreSQL 18 reference · String Functions

PostgreSQL MD5() Function

Return the MD5 hash of a string as 32 hex characters. Fine for checksums, never for passwords.

PostgreSQL 18

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

md5(string)

Parameters

ParameterDescription
string The input text.

Return value

Text (32 hex digits).

Examples

Hash a string
SELECT md5('hello') AS hash;

Output

               hash
----------------------------------
 5d41402abc4b2a76b9719d911017c592
(1 row)

Run this on your computer — see “Get Started” for setup.

Short, stable keys
SELECT name, left(md5(email), 8) AS email_key
FROM customers
WHERE email IS NOT NULL
LIMIT 2;

Output

     name     | email_key
--------------+-----------
 Ada Lovelace | 3e3417d7
 Alan Turing  | f8982b84
(2 rows)

Run this on your computer — see “Get Started” for setup.

For security use sha256(convert_to(text, 'UTF8')) (PostgreSQL 11+), and store passwords with a slow hash such as crypt() from the pgcrypto extension.

Was this page helpful?