9.14. UUID Functions #

PostgreSQL includes several functions to generate a UUID.

gen_random_uuid () → uuid
uuidv4 () → uuid

These functions return a version 4 (random) UUID.

uuidv7 ([ shift interval ]) → uuid

This function returns a version 7 UUID (UNIX timestamp with millisecond precision + sub-millisecond timestamp + random). This function can accept optional shift parameter of type interval which shift internal timestamp by the given interval.

The uuid-ossp module provides additional functions that implement other standard algorithms for generating UUIDs.

There are also functions to extract data from UUIDs:

uuid_extract_timestamp (uuid) → timestamp with time zone

This function extracts a timestamp with time zone from UUID version 1 and 7. For other versions, this function returns null. Note that the extracted timestamp is not necessarily exactly equal to the time the UUID was generated; this depends on the implementation that generated the UUID.

uuid_extract_version (uuid) → smallint

This function extracts the version from a UUID of the variant described by RFC 9562. For other variants, this function returns null. For example, for a UUID generated by gen_random_uuid, this function will return 4.

PostgreSQL also provides the usual comparison operators shown in Table 9.1 for UUIDs.