UUID (universally unique identifier)

Last updated: 2026-11-04

UUID (universally unique identifier)

UUID is a 128-bit universally unique identifier. PostgreSQL stores it as 16 bytes and compares it by byte order rather than string order, which keeps both storage and comparison efficient. Since PostgreSQL 13 the built-in gen_random_uuid() generates version 4 (random) UUIDs with no extension required. UUIDs are the safe default when data must be merged across several systems, or when exposing auto-increment IDs is undesirable.

Kindtype
Example LanguageSQL

📝 Syntax

UUID  ——  16 bytes, canonical 8-4-4-4-12 form
--  generation:
gen_random_uuid()                    -- version 4, random (built in since PG13)
--  legacy approach needs an extension: CREATE EXTENSION "uuid-ossp"; uuid_generate_v4()

Returns:Stores a 128-bit UUID. A valid string can be converted with ::uuid, while malformed input raises "invalid input syntax for type uuid". gen_random_uuid() returns a random version 4 UUID.

▶ 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:

                  id                  |   name   | matches_fixed | gen_len | gen_version | col_type
--------------------------------------+----------+---------------+---------+-------------+----------
 a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11 | fixed id | t             |      36 | 4           | uuid

📌 Key Points

More case studies coming soon.

📚 Related Reference

❓ FAQ

QDoes a UUID primary key slow the index down?
AYes, random UUIDs scatter writes and make the B-tree split constantly. Workarounds include keeping an auto-increment BIGINT primary key with the UUID as a separately indexed unique column, or using ordered UUIDs such as UUIDv7.
QDo UUID and gen_random_uuid() need an extension?
ANo. gen_random_uuid() is built in from PostgreSQL 13 onwards and is available in the browser-side PGlite as well. Only older versions need CREATE EXTENSION "uuid-ossp".
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%

🙏 帮我们做得更好

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

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