IDENTITY (SQL-standard identity column)
Last updated: 2026-11-04
IDENTITY (SQL-standard identity column)
IDENTITY is the SQL-standard mechanism for auto-increment columns, supported by PostgreSQL since version 10. You append GENERATED ALWAYS AS IDENTITY or GENERATED BY DEFAULT AS IDENTITY to the column definition, and PostgreSQL creates an internal sequence bound to that column. Compared with SERIAL, the identity sequence is an internal dependency of the column, is cleaned up with the table, can be reset with TRUNCATE ... RESTART IDENTITY, and GENERATED ALWAYS rejects manually supplied values.
| Kind | type |
|---|---|
| Example Language | SQL |
📝 Syntax
id integer GENERATED ALWAYS AS IDENTITY [ ( sequence_options ) ]
id integer GENERATED BY DEFAULT AS IDENTITY [ ( sequence_options ) ]
-- sequence_options e.g. START WITH 1 INCREMENT BY 1
Returns:The storage type of an identity column is whatever the column declares (usually INTEGER or BIGINT); the values come from the bound internal sequence, whose options can be written as (START WITH 100 INCREMENT BY 5).
▶ Example
Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.
Output:
id | name | data_type | is_identity | identity_generation
----+-------+-----------+-------------+---------------------
1 | Alice | integer | YES | ALWAYS
2 | Bob | integer | YES | ALWAYS
📌 Key Points
- First difference from SERIAL: the identity sequence is an internal dependency of the column and disappears with it, whereas a SERIAL sequence is a standalone object that is likewise dropped with the table but can still be referenced with nextval().
- Second difference: GENERATED ALWAYS rejects explicitly supplied values (OVERRIDING SYSTEM VALUE bypasses it), while SERIAL accepts a manual id without advancing the sequence.
- Third difference: the policy can be switched at any time with ALTER TABLE t ALTER COLUMN id SET GENERATED BY DEFAULT.