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
Syntax
uuidv7() | uuidv7(shift interval)
Parameters
| Parameter | Description |
|---|---|
shift optional |
Shift the embedded timestamp by this interval. |
Return value
uuid.
Examples
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.
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
| Version | Change |
|---|---|
| PostgreSQL 18 | Added |
Related
GEN_RANDOM_UUID()· Generate a random version 4 UUID. Since PostgreSQL 18, uuidv4() is another name for it.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