PostgreSQL 18 reference · UUID Functions

PostgreSQL UUIDV7() Function

Generate a version 7 UUID, which starts with a timestamp so new IDs sort in creation order. Added in PostgreSQL 18.

PostgreSQL 18 Since 18

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

Syntax

uuidv7()  |  uuidv7(shift interval)

Parameters

ParameterDescription
shift optional Shift the embedded timestamp by this interval.

Return value

uuid.

Examples

Time-ordered IDs
SELECT uuid_extract_version(uuidv7()) AS version, uuidv7() < uuidv7() AS increasing;

Output

 version | increasing
---------+------------
       7 | t
(1 row)

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

Shift the timestamp
SELECT uuid_extract_timestamp(uuidv7(interval '-1 day')) < now() - interval '23 hours' AS shifted_back;

Output

 shifted_back
--------------
 t
(1 row)

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

Version 7 UUIDs are good primary keys: they're unique without coordination like v4, but insert into B-tree indexes in order, which keeps indexes compact.

Version history

VersionChange
PostgreSQL 18Added

Was this page helpful?