uuid-ossp: UUID Generation Extension
Last updated: 2026-11-04
uuid-ossp: UUID Generation Extension
uuid-ossp provides UUID generators such as uuid_generate_v4() and uuid_generate_v1(), plus helpers like uuid_extract_version() and uuid_nil(). Since PostgreSQL 13 the core ships gen_random_uuid(), so plain random v4 UUIDs no longer require this extension. What it still uniquely offers is time-ordered uuid_generate_v1() UUIDs (handy as a range partition key) and the ability to read a UUID's version back out.
| Kind | extension |
|---|---|
| Example Language | SQL |
📝 Syntax
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
SELECT uuid_generate_v4(); -- random v4
SELECT uuid_generate_v1(); -- time-ordered v1
SELECT uuid_extract_version(uuid); -- read the version (4 / 1)
SELECT uuid_nil(); -- the all-zero UUID
Returns:CREATE EXTENSION returns success on completion. The functions it installs return the uuid type; uuid_extract_version() returns the version as a smallint, and uuid_nil() returns the all-zero UUID (a valid uuid value, not NULL).
▶ Example
Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.
Output:
SELECT length(uuid_generate_v4()::text) AS uuid_text_len,
uuid_extract_version(uuid_generate_v1()) AS v1_version,
uuid_nil() AS nil_uuid
uuid_text_len | v1_version | nil_uuid
---------------+------------+----------------------------------
36 | 1 | 00000000-0000-0000-0000-000000000000
(1 row)
SELECT label,
uuid_extract_version(id) AS ossp_version,
uuid_extract_version(builtin_id) AS builtin_version
FROM t_uuid_demo ORDER BY label
label | ossp_version | builtin_version
-------+--------------+----------------
alpha | 4 | 4
beta | 4 | 4
gamma | 4 | 4
(3 rows)
Note: the builtin_id column is produced by the core gen_random_uuid() available since PG 13+; both columns report version 4, confirming that either call yields a random v4 UUID.
📌 Key Points
- In browser PGlite it must be loaded on demand: import { uuid_ossp } from '@electric-sql/pglite/contrib/uuid_ossp', then new PGlite({ extensions: { uuid_ossp } }). Note the underscore in the import name uuid_ossp versus the hyphenated extension name uuid-ossp.
- Size: the uuid-ossp contrib bundle is about 16.6 KB, in the same lightweight league as pg_trgm (~15 KB) and btree_gist (~22 KB); pgcrypto is around 1.1 MB and is conspicuously the largest of the bunch.
- PG 13+ ships gen_random_uuid() in core, so generating random v4 UUIDs no longer needs an extension; uuid-ossp remains irreplaceable for uuid_generate_v1() (time-ordered, good as a range partition key) and uuid_extract_version() (reading a UUID's version back).
- The extension name contains a hyphen, so CREATE EXTENSION must quote it as "uuid-ossp".
- Installing requires CREATE privilege on the current database; DROP EXTENSION "uuid-ossp" also removes the functions and column defaults that depend on it.
- uuid_nil() returns the all-zero UUID. That is a valid uuid value, so test for presence with IS NULL rather than by comparing against uuid_nil().