UUID generator (UUID-OSSP)
The UUID-OSSP extension provides functions to generate universally unique identifiers (UUIDs) in ApsaraDB RDS for PostgreSQL. Use it when you need version 1, 3, or 5 UUIDs.
Prerequisites
Before you begin, ensure that you have:
A privileged account on your ApsaraDB RDS for PostgreSQL instance
A PostgreSQL client connected to your instance
Enable the extension
Run the following statement as a privileged account:
CREATE EXTENSION "uuid-ossp";To remove the extension:
DROP EXTENSION "uuid-ossp";Choose a UUID version
| Version | Function | Algorithm | Use when |
|---|---|---|---|
| v4 | uuid_generate_v4() → uuid | Random numbers | General-purpose unique IDs, primary keys — recommended for most cases |
| v5 | uuid_generate_v5(namespace uuid, name text) → uuid | SHA-1 | Deterministic IDs derived from a name; prefer v5 over v3 |
| v3 | uuid_generate_v3(namespace uuid, name text) → uuid | MD5 | Deterministic IDs when v5 is not available |
| v1mc | uuid_generate_v1mc() → uuid | Timestamp + random multicast MAC address | Time-ordered IDs without exposing the real MAC address |
| v1 | uuid_generate_v1() → uuid | Timestamp + real MAC address | Avoid in most cases — exposes machine identity and generation time |
Quick guidance:
Use v4 for primary keys and general unique identifiers.
Use v5 (not v3) when you need IDs reproducible from the same input — for example, generating the same UUID for a given URL every time.
Use v1mc instead of v1 when time-based ordering matters. Avoid both in security-sensitive applications.
Generate UUIDs
Version 4 — random (recommended)
uuid_generate_v4() → uuid
SELECT uuid_generate_v4();Output:
uuid_generate_v4
--------------------------------------
170d0eb6-520a-4f93-a1b3-89458fffb54c
(1 row)Use a version 4 UUID as the default primary key in a table:
CREATE TABLE contacts (
id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,
first_name text,
last_name text
);Version 5 — deterministic with SHA-1
uuid_generate_v5(namespace uuid, name text) → uuid
The namespace parameter must be one of the constants returned by the uuid_ns_*() functions (see UUID namespace constants). The name parameter is an identifier within that namespace.
The same namespace and name always produce the same UUID. No plaintext can be derived from the output.
SELECT uuid_generate_v5(uuid_ns_url(), 'example.com');Output:
uuid_generate_v5
--------------------------------------
a5cf6e8e-4cfa-5f31-a804-6de6d1245e26
(1 row)Version 3 — deterministic with MD5
uuid_generate_v3(namespace uuid, name text) → uuid
Works the same way as v5, but uses MD5 instead of SHA-1. Use v5 when possible.
SELECT uuid_generate_v3(uuid_ns_url(), 'example.com');Output:
uuid_generate_v3
--------------------------------------
a0473a67-27a1-3c05-a2d1-5c134639347f
(1 row)Version 1 — timestamp-based
uuid_generate_v1() → uuid
SELECT uuid_generate_v1();Output:
uuid_generate_v1
--------------------------------------
a6808efc-13c8-11ed-ad4f-00163e010e52
(1 row)Version 1 UUIDs embed the real MAC address of the database server and the exact generation timestamp. This makes the generating machine identifiable and the UUID partially predictable. Use uuid_generate_v1mc() instead if you need time-ordering without exposing the real MAC address.uuid_generate_v1mc() → uuid
SELECT uuid_generate_v1mc();This function uses a random multicast MAC address instead of the real MAC address, preserving time-ordering while avoiding machine identification.
UUID namespace constants
Use these constants as the namespace argument in uuid_generate_v3() and uuid_generate_v5().
| Function | Returns | Description |
|---|---|---|
uuid_ns_dns() | uuid | Domain Name System (DNS) namespace |
uuid_ns_url() | uuid | Uniform Resource Locator (URL) namespace |
uuid_ns_oid() | uuid | ISO object identifier (OID) namespace, defined by the Abstract Syntax Notation One (ASN.1) standard — different from PostgreSQL OIDs |
uuid_ns_x500() | uuid | X.500 distinguished name (DN) namespace |
UUID constants
| Function | Returns | Description |
|---|---|---|
uuid_nil() | uuid | Nil UUID constant — not a real UUID |
UUID format
A standard UUID has 32 hexadecimal digits and 4 hyphens, totaling 36 characters. The digits are arranged in five groups: 8-4-4-4-12.
a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11PostgreSQL also accepts UUIDs in these alternative formats:
| Format | Example |
|---|---|
| Uppercase | A0EEBC99-9C0B-4EF8-BB6D-6BB9BD380A11 |
| Braces | {a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11} |
| No hyphens | a0eebc999c0b4ef8bb6d6bb9bd380a11 |
| 4-digit groups | a0ee-bc99-9c0b-4ef8-bb6d-6bb9-bd38-0a11 |