← All notes

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

Sources

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

Permanent ID: iil:note:cs5q7y63

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.},
}

Markdown

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.