PostgreSQL 18 reference · UUID Functions
PostgreSQL GEN_RANDOM_UUID() Function
Generate a random version 4 UUID. Since PostgreSQL 18, uuidv4() is another name for it.
PostgreSQL 18 Since 13
Syntax
gen_random_uuid() | uuidv4()
Return value
uuid.
Examples
SELECT length(gen_random_uuid()::text) AS chars, gen_random_uuid() <> gen_random_uuid() AS always_different;
Output
chars | always_different
-------+------------------
36 | t
(1 row)Run this on your computer — see “Get Started” for setup.
CREATE TABLE tickets (id uuid PRIMARY KEY DEFAULT gen_random_uuid(), title text);
INSERT INTO tickets (title) VALUES ('Printer jam'), ('VPN down');
SELECT count(DISTINCT id) AS unique_ids FROM tickets;
Output
unique_ids
------------
2
(1 row)Run this on your computer — see “Get Started” for setup.
Version history
| Version | Change |
|---|---|
| PostgreSQL 13 | Added |
| PostgreSQL 18 | Added uuidv4() as another name. |
Related
UUIDV7()· Generate a version 7 UUID, which starts with a timestamp so new IDs sort in creation order. Added in PostgreSQL 18.UUID_EXTRACT_TIMESTAMP()· Return the creation time stored inside a version 1 or 7 UUID, or NULL for other versions. Added in PostgreSQL 17.- SQL String Functions · tutorial