Skip to content

to_uuidv7()

Create a version 7 UUID from a Postgres timestamp

Since 2.23.0

Create a UUIDv7 object from a PostgreSQL timestamp and random bits.

ts is converted to a UNIX timestamp split into millisecond and sub-millisecond parts.

UUIDv7 structure with microsecond-precision timestamp
SELECT to_uuidv7(ts)
FROM generate_series('2025-01-01:00:00:00'::timestamptz, '2025-01-01:00:00:03'::timestamptz, '1 microsecond'::interval) ts;

The syntax is:

SELECT to_uuidv7(
timestamp = <timestamptz>
);
NameTypeDefaultRequiredDescription
tsTIMESTAMPTZ-The timestamp used to return a UUIDv7 object
ColumnTypeDescription
to_uuidv7UUIDA UUIDv7 object created from the input timestamp with random bits.