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

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

Syntax

gen_random_uuid()  |  uuidv4()

Return value

uuid.

Examples

A random UUID
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.

As a column default
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

VersionChange
PostgreSQL 13Added
PostgreSQL 18Added uuidv4() as another name.

Was this page helpful?