4 Table 9.45 shows the PostgreSQL functions that can be used to generate
7 Table 9.45. UUID Generation Functions
15 gen_random_uuid → uuid
19 Generate a version 4 (random) UUID.
21 gen_random_uuid() → 5b30857f-0bfa-48b5-ac0b-5c64e28078d1
23 uuidv4() → b42410ee-132f-42ee-9e4f-09a6485c95b8
25 uuidv7 ( [ shift interval ] ) → uuid
27 Generate a version 7 (time-ordered) UUID. The timestamp is computed
28 using UNIX timestamp with millisecond precision + sub-millisecond
29 timestamp + random. The optional parameter shift will shift the
30 computed timestamp by the given interval.
32 uuidv7() → 019535d9-3df7-79fb-b466-fa907fa17f9e
36 The uuid-ossp module provides additional functions that implement other
37 standard algorithms for generating UUIDs.
39 Table 9.46 shows the PostgreSQL functions that can be used to extract
40 information from UUIDs.
42 Table 9.46. UUID Extraction Functions
50 uuid_extract_timestamp ( uuid ) → timestamp with time zone
52 Extracts a timestamp with time zone from UUID version 1 and 7. For
53 other versions, this function returns null. Note that the extracted
54 timestamp is not necessarily exactly equal to the time the UUID was
55 generated; this depends on the implementation that generated the UUID.
57 uuid_extract_timestamp('019535d9-3df7-79fb-b466-fa907fa17f9e'::uuid) →
58 2025-02-23 21:46:24.503-05
60 uuid_extract_version ( uuid ) → smallint
62 Extracts the version from a UUID of the variant described by RFC 9562.
63 For other variants, this function returns null. For example, for a UUID
64 generated by gen_random_uuid, this function will return 4.
66 uuid_extract_version('41db1265-8bc1-4ab3-992f-885799a4af1d'::uuid) → 4
68 uuid_extract_version('019535d9-3df7-79fb-b466-fa907fa17f9e'::uuid) → 7
70 PostgreSQL also provides the usual comparison operators shown in
73 See Section 8.12 for details on the data type uuid in PostgreSQL.