UUIDs in PostgreSQL
Answer: PostgreSQL has a native uuid type. Generate random version 4 UUIDs with built-in gen_random_uuid(), or time-ordered version 7 UUIDs with uuidv7() on recent versions.
CREATE TABLE orders (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
created timestamptz DEFAULT now()
);
SELECT gen_random_uuid(); -- e.g. 5b30857f-0bfa-48b5-ac0b-5c64e28078d1
SELECT uuidv7();
Input formats
Besides the standard hyphenated lower-case form, PostgreSQL accepts upper-case digits, braces, and missing or extra hyphens.
uuid vs bigint keys
Claims
- id: C1 claim: PostgreSQL accepts upper-case digits and braces in UUID input. source: S1 evidence: "use of upper-case digits, the standard format surrounded by braces"
- id: C2 claim: gen_random_uuid generates a version 4 (random) UUID. source: S2 evidence: "Generates a version 4 (random) UUID"
- id: C3 claim: uuid-ossp provides additional UUID algorithms. source: S2 evidence: "The uuid-ossp module provides additional functions that implement other standard algorithms"
- id: C4 claim: PostgreSQL provides the usual comparison operators for UUIDs. source: S2 evidence: "PostgreSQL also provides the usual comparison operators shown in Table 9.1 for UUIDs."
Sources
- [S1] PostgreSQL: Documentation: 18: 8.12. UUID Type. PostgreSQL Global Development Group. https://www.postgresql.org/docs/current/datatype-uuid.html . License: PostgreSQL License. Accessed 2026-11-04.
- [S2] PostgreSQL: Documentation: 18: 9.14. UUID Functions. PostgreSQL Global Development Group. https://www.postgresql.org/docs/current/functions-uuid.html . License: PostgreSQL License. Accessed 2026-11-04.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted).
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: PostgreSQL Global Development Group, PostgreSQL: Documentation: 18: 8.12. UUID Type, PostgreSQL License; PostgreSQL Global Development Group, PostgreSQL: Documentation: 18: 9.14. UUID Functions, PostgreSQL License.
Cite this
iinventedlight. 2026. "UUIDs in PostgreSQL." iinventedlight Library, iil:note:cs5q7y63, version of 2026-11-04. https://iinventedlight.com/library/notes/postgres-uuid/. License: CC-BY-4.0.
@misc{iil_note_cs5q7y63,
author = {{iinventedlight}},
title = {{UUIDs in PostgreSQL}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-04},
url = {https://iinventedlight.com/library/notes/postgres-uuid/},
note = {Permanent ID iil:note:cs5q7y63. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.
For language models and agents: start at /llms.txt. This page in Markdown: /library/notes/postgres-uuid.md.