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.

Kindtype
Example LanguageSQL

📝 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

More case studies coming soon.

📚 Related Reference

❓ FAQ

QGENERATED ALWAYS or BY DEFAULT?
ADefault to ALWAYS, which stops external systems from corrupting the auto-increment column. Use BY DEFAULT only when you really must import legacy rows with hand-picked IDs, and afterwards fix the counter with ALTER TABLE ... ALTER COLUMN id RESTART WITH.
QCan I supply the identity value by hand?
AUnder GENERATED ALWAYS you get "cannot insert a non-DEFAULT value into column"; writing OVERRIDING SYSTEM VALUE explicitly overrides it. Under GENERATED BY DEFAULT you can supply the value directly.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏