---
title: "PostgreSQL identity columns for auto-generated keys"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source: https://iinventedlight.com/library/notes/postgres-identity-columns.md
source_license: "PostgreSQL License; restated in our own words"
attribution: "Summary by iinventedlight (AI-assisted). Sources: PostgreSQL Global Development Group, PostgreSQL: Documentation: 18: 5.3. Identity Columns, PostgreSQL License; PostgreSQL Global Development Group, PostgreSQL: Documentation: 18: CREATE TABLE, 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; only the fetched documentation pages were read."
category: coding-references
topic_area: "postgresql: keys"
tags: postgresql, sql, identity, primary-key
sources:
  - id: S1
    title: "PostgreSQL: Documentation: 18: 5.3. Identity Columns"
    publisher: PostgreSQL Global Development Group
    url: https://www.postgresql.org/docs/current/ddl-identity-columns.html
    accessed: 2026-11-04
    license: "PostgreSQL License"
  - id: S2
    title: "PostgreSQL: Documentation: 18: CREATE TABLE"
    publisher: PostgreSQL Global Development Group
    url: https://www.postgresql.org/docs/current/sql-createtable.html
    accessed: 2026-11-04
    license: "PostgreSQL License"
---
# PostgreSQL identity columns for auto-generated keys

## Short answer
Use `id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY` (or `BY DEFAULT`) for auto-numbered keys. Values come from an implicit sequence when you insert without an id. [S1]

## ALWAYS vs BY DEFAULT
These clauses set how a user-supplied id is handled: `ALWAYS` refuses it unless you override, `BY DEFAULT` accepts it. [S1][S2] Prefer `ALWAYS` to keep keys consistent.

## Claims
- id: C1
  claim: Identity columns are generated from an implicit sequence.
  source: S1
  evidence: "generated automatically from an implicit sequence"
- id: C2
  claim: Identity columns are created with GENERATED AS IDENTITY.
  source: S1
  evidence: "use the GENERATED ... AS IDENTITY clause in CREATE TABLE"
- id: C3
  claim: ALWAYS and BY DEFAULT control user-supplied values.
  source: S1
  evidence: "determine how explicitly user-specified values are handled"
- id: C4
  claim: CREATE TABLE defines a new table.
  source: S2
  evidence: "CREATE TABLE — define a new table"

## Sources
- [S1] PostgreSQL: Documentation: 18: 5.3. Identity Columns. PostgreSQL Global Development Group. https://www.postgresql.org/docs/current/ddl-identity-columns.html . License: PostgreSQL License. Accessed 2026-11-04.
- [S2] PostgreSQL: Documentation: 18: CREATE TABLE. PostgreSQL Global Development Group. https://www.postgresql.org/docs/current/sql-createtable.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).
