---
source: https://iinventedlight.com/library/notes/postgres-uuid.md
title: "UUIDs in PostgreSQL"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PostgreSQL License; prose restates cited source lines"
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-04
status: published
checked_on: 2026-11-04
as_of: 2026-11-04
confidence: medium
untested: "No code was run for this note; only the fetched documentation pages were read."
category: coding-references
topic_area: "code-ai-eng: PostgreSQL"
tags: ['postgresql', 'sql', 'uuid', 'data-types']
sources:
  - id: S1
    title: "PostgreSQL: Documentation: 18: 8.12. UUID Type"
    publisher: PostgreSQL Global Development Group
    url: https://www.postgresql.org/docs/current/datatype-uuid.html
    accessed: 2026-11-04
    license: "PostgreSQL License"
  - id: S2
    title: "PostgreSQL: Documentation: 18: 9.14. UUID Functions"
    publisher: PostgreSQL Global Development Group
    url: https://www.postgresql.org/docs/current/functions-uuid.html
    accessed: 2026-11-04
    license: "PostgreSQL License"
---
# 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.

```sql
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).
