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.

Kindextension
Example LanguageSQL

📝 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

More case studies coming soon.

📚 Related Reference

❓ FAQ

QDo I still need uuid-ossp after PostgreSQL 13?
ANot for plain random v4 UUIDs: the built-in gen_random_uuid() ships with the core and needs no extension. uuid-ossp is still worth installing when you need uuid_generate_v1() (time-ordered UUIDs) or want uuid_extract_version() to read a version back.
QAre uuid_generate_v4() and gen_random_uuid() different?
ABoth produce random v4 UUIDs from the same OS randomness source and share the same format, so neither is predictable from the other. The difference is ownership: gen_random_uuid() is a core function since PG 13+ and is upgraded with the server, while uuid_generate_v4() lives in the uuid-ossp extension and can be installed and dropped independently.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏