← All notes

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

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: 5.3. Identity Columns, PostgreSQL License; PostgreSQL Global Development Group, PostgreSQL: Documentation: 18: CREATE TABLE, PostgreSQL License.

Cite this

Permanent ID: iil:note:8epj0299

iinventedlight. 2026. "PostgreSQL identity columns for auto-generated keys." iinventedlight Library, iil:note:8epj0299, version of 2026-11-04. https://iinventedlight.com/library/notes/postgres-identity-columns/. License: CC-BY-4.0.

@misc{iil_note_8epj0299,
  author = {{iinventedlight}},
  title = {{PostgreSQL identity columns for auto-generated keys}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-04},
  url = {https://iinventedlight.com/library/notes/postgres-identity-columns/},
  note = {Permanent ID iil:note:8epj0299. 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-identity-columns.md.