IDENTITY Column
Last updated: 2026-11-04
IDENTITY Column
IDENTITY is the SQL-standard way to declare an auto-incrementing column. Unlike SERIAL, which attaches a separate sequence and uses nextval as a column default, IDENTITY binds the column and its sequence as one standard object. ALWAYS forbids manual values while BY DEFAULT permits them, and both are understood more reliably by tooling than SERIAL.
| Kind | constraint |
|---|---|
| Example Language | SQL |
📝 Syntax
`column_name type GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY`
Options: `START WITH n` / `INCREMENT BY n`
Explicit write: `INSERT INTO t (id, ...) OVERRIDING SYSTEM VALUE VALUES (...)`
⚙️ Parameters
| ALWAYS / BY DEFAULT | ALWAYS rejects any explicit value (unless OVERRIDING SYSTEM VALUE is used); BY DEFAULT allows explicit values, generating one only when the column is omitted. |
|---|---|
| START WITH / INCREMENT BY | The sequence's starting value and step, with the same semantics as CREATE SEQUENCE. |
Returns:IDENTITY has no return value of its own. It is bound to an internal sequence whose name pg_get_serial_sequence() can reveal.
▶ 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:
SELECT 'BY DEFAULT -> auto ids' AS step,
(SELECT string_agg(ticket_id::TEXT, ', ' ORDER BY ticket_id) FROM tickets) AS result
UNION ALL SELECT 'ALWAYS -> explicit id rejected',
(SELECT pg_try('INSERT INTO strict_tickets (ticket_id, title) VALUES (100, ''Manual'')'))
UNION ALL SELECT 'ALWAYS -> OVERRIDING SYSTEM VALUE',
(SELECT pg_try('INSERT INTO strict_tickets (ticket_id, title) OVERRIDING SYSTEM VALUE VALUES (100, ''Manual'')'))
UNION ALL SELECT 'backing sequence',
(SELECT pg_get_serial_sequence('tickets','ticket_id'))
step | result
-----------------------------------+-----------------------------------------------------------
BY DEFAULT -> auto ids | 1, 2
ALWAYS -> explicit id rejected | cannot insert a non-DEFAULT value into column "ticket_id"
ALWAYS -> OVERRIDING SYSTEM VALUE | OK
backing sequence | public.tickets_ticket_id_seq
(4 rows)
📌 Key Points
- IDENTITY is preferable to SERIAL: it is standard syntax, tooling recognizes the column-to-sequence binding, and schema migration tools handle it more reliably.
- Under ALWAYS, an explicit value raises `cannot insert a non-DEFAULT value into column "..."`; add OVERRIDING SYSTEM VALUE when you genuinely need one.
- The underlying sequence can be reset with ALTER TABLE ... ALTER COLUMN ... RESTART WITH.
- An IDENTITY column also carries NOT NULL semantics and cannot be set to NULL.
💡 Related Cases
More case studies coming soon.
📚 Related Reference
❓ FAQ
QShould I use IDENTITY or SERIAL?
APrefer IDENTITY for new work: it is standard, models the column-as-identity idea directly, and needs no special handling when serializing foreign keys (PostgreSQL 10+). Existing SERIAL columns need no forced migration and behave identically.
QHow do I choose between BY DEFAULT and ALWAYS?
ABY DEFAULT permits explicit IDs, which suits bulk imports that must preserve original identifiers. ALWAYS insists the database owns identity, which fits strict append-only ledgers where outside interference is not wanted.