Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
DocumentationPostgreSQL devel (2025-04-26 15:42:29 - git commit 10e8176950b)
Supported Versions: Current (17) / 16 / 15 / 14 / 13
Development Versions: devel
This documentation is for an unsupported version of PostgreSQL.
You may want to view the same page for the current version, or one of the other supported versions listed above instead.

9.14. UUID Functions #

Table 9.45 shows the PostgreSQL functions that can be used to generate UUIDs.

Table 9.45. UUID Generation Functions

Function

Description

Example(s)

gen_random_uuiduuid

uuidv4uuid

Generate a version 4 (random) UUID.

gen_random_uuid()5b30857f-0bfa-48b5-ac0b-5c64e28078d1

uuidv4()b42410ee-132f-42ee-9e4f-09a6485c95b8

uuidv7 ( [ shift interval ] ) → uuid

Generate a version 7 (time-ordered) UUID. The timestamp is computed using UNIX timestamp with millisecond precision + sub-millisecond timestamp + random. The optional parameter shift will shift the computed timestamp by the given interval.

uuidv7()019535d9-3df7-79fb-b466-fa907fa17f9e


Note

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

Table 9.46 shows the PostgreSQL functions that can be used to extract information from UUIDs.

Table 9.46. UUID Extraction Functions

Function

Description

Example(s)

uuid_extract_timestamp ( uuid ) → timestamp with time zone

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_timestamp('019535d9-3df7-79fb-b466-​fa907fa17f9e'::uuid)2025-02-23 21:46:24.503-05

uuid_extract_version ( uuid ) → smallint

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.

uuid_extract_version('41db1265-8bc1-4ab3-992f-​885799a4af1d'::uuid)4

uuid_extract_version('019535d9-3df7-79fb-b466-​fa907fa17f9e'::uuid)7


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

See Section 8.12 for details on the data type uuid in PostgreSQL.