The uuid-ossp extension provides functions for generating universally unique identifiers (UUIDs). In distributed systems, UUIDs offer stronger uniqueness guarantees than sequences, making them a reliable choice for primary keys and record identifiers across nodes.
UUID format
A UUID consists of 32 hexadecimal digits arranged as five groups separated by hyphens: 8-4-4-4-12. For example:
a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11PostgreSQL accepts UUIDs in several equivalent formats:
Format | Example |
Standard (lowercase) |
|
Uppercase |
|
Braces |
|
No hyphens |
|
Hyphens after every 4 digits |
|
Mixed |
|
UUID fields cannot be used as the distribution key in AnalyticDB for PostgreSQL.
Install the extension
Before generating UUIDs, install the uuid-ossp extension from the Extensions page of your instance. For details, see Install, update, and uninstall extensions.
UUID generation functions
Version 1: time-based
uuid_generate_v1() generates a UUID from the current timestamp and the host machine's MAC address.
uuid_generate_v1mc() works like uuid_generate_v1() but uses a random multicast MAC address instead of the real MAC address, avoiding direct exposure of the machine's hardware identifier.
Version 1 UUIDs embed the MAC address of the generating machine and include a time component, making them somewhat predictable. Avoid version 1 UUIDs in security-sensitive applications.
Version 3: name-based (MD5)
uuid_generate_v3(namespace uuid, name text) generates a deterministic UUID by hashing name within namespace using MD5.
namespace: a constant returned by one of theuuid_ns_*()functions (see Namespace constants).name: an identifier within the namespace.
SELECT uuid_generate_v3(uuid_ns_url(), 'example.com');The same inputs always produce the same UUID. No random algorithm or system environment is involved, so the result is fully reproducible.
Version 4: random
uuid_generate_v4() generates a UUID based on random numbers.
Version 5: name-based (SHA-1)
uuid_generate_v5(namespace uuid, name text) works like uuid_generate_v3() but uses SHA-1 instead of MD5. SHA-1 is more secure than MD5, so prefer version 5 over version 3 whenever you need name-based UUIDs.
Namespace constants
Use these constants as the namespace argument in uuid_generate_v3() and uuid_generate_v5().
Function | Namespace |
| Domain Name System (DNS) |
| URL |
| ISO object identifier (OID) — defined by the Abstract Syntax Notation One (ASN.1) standard; different from PostgreSQL OIDs |
| X.500 distinguished name (DN) |
uuid_nil() returns a nil UUID constant, which is not a real UUID.
Examples
-- Version 1 (time-based)
SELECT uuid_generate_v1(); uuid_generate_v1
--------------------------------------
c7f83ba4-bd93-11e9-8674-40a8f01ec4e8
(1 row)-- Version 3 (name-based, MD5)
SELECT uuid_generate_v3(uuid_ns_url(), 'example.com'); uuid_generate_v3
--------------------------------------
a0473a67-27a1-3c05-a2d1-5c134639347f
(1 row)-- Version 4 (random)
SELECT uuid_generate_v4(); uuid_generate_v4
--------------------------------------
d7a8d47e-58e3-4bd9-9340-8553ac03d144
(1 row)-- Version 5 (name-based, SHA-1)
SELECT uuid_generate_v5(uuid_ns_url(), 'example.com'); uuid_generate_v5
--------------------------------------
a5cf6e8e-4cfa-5f31-a804-6de6d1245e26
(1 row)