Both give a column numbers that count up by themselves: 1, 2, 3. An identity column is the SQL-standard way, and serial is PostgreSQL’s older shorthand. Here’s identity vs serial in PostgreSQL, and why an identity column is the better choice for IDs.
Every command and output on this page is real, from PostgreSQL 19 on macOS.
Syntax
column_name integer GENERATED ALWAYS AS IDENTITY
column_name integer GENERATED BY DEFAULT AS IDENTITY
column_name serial
GENERATED ALWAYS AS IDENTITY: PostgreSQL always picks the number, and refuses one you type.GENERATED BY DEFAULT AS IDENTITY: PostgreSQL picks the number unless you give one.serial: not a real type, but a shorthand for anintegercolumn with a default taken from a sequence, a counter object PostgreSQL creates for it.- Use
bigintinstead ofintegerfor a table that could pass 2 billion rows;bigserialis the serial form.
See Identity Columns (opens in a new tab) and Serial Types (opens in a new tab) in the PostgreSQL documentation.
The Example
A help desk numbers its tickets. It tries both ways, in two small tables side by side.
Step 1: Create a Test Database
Connect to your server with psql as the postgres user, as Part 2 of the PostgreSQL Tutorial shows. psql connects you to the postgres database, and its prompt is postgres=#.
Create a database for the example and connect to it:
CREATE DATABASE helpdesk;
CREATE DATABASE
\c helpdesk
You are now connected to database "helpdesk" as user "postgres".
Step 2: Create Both Tables
CREATE TABLE tickets_serial (id serial, subject text);
CREATE TABLE
CREATE TABLE tickets_identity (id integer GENERATED ALWAYS AS IDENTITY, subject text);
CREATE TABLE
Step 3: Add a Ticket to Each
Leave out the id, and PostgreSQL fills it in:
INSERT INTO tickets_serial (subject) VALUES ('Printer is jammed');
INSERT 0 1
INSERT INTO tickets_identity (subject) VALUES ('Printer is jammed');
INSERT 0 1
Step 4: Type an ID Yourself
Now give each table an id of your own, 1, which is already taken. The serial table accepts it:
INSERT INTO tickets_serial (id, subject) VALUES (1, 'Screen is blank');
INSERT 0 1
The identity table refuses it:
INSERT INTO tickets_identity (id, subject) VALUES (1, 'Screen is blank');
ERROR: cannot insert a non-DEFAULT value into column "id"
DETAIL: Column "id" is an identity column defined as GENERATED ALWAYS.
HINT: Use OVERRIDING SYSTEM VALUE to override.
Look at both tables:
SELECT * FROM tickets_serial;
id | subject
----+-------------------
1 | Printer is jammed
1 | Screen is blank
(2 rows)
SELECT * FROM tickets_identity;
id | subject
----+-------------------
1 | Printer is jammed
(1 row)
The serial table now has two tickets numbered 1. GENERATED ALWAYS stopped that mistake before it happened.
Step 5: See How Each Column Is Defined
psql’s \d command describes a table:
\d tickets_serial
Table "public.tickets_serial"
Column | Type | Collation | Nullable | Default
---------+---------+-----------+----------+--------------------------------------------
id | integer | | not null | nextval('tickets_serial_id_seq'::regclass)
subject | text | | |
\d tickets_identity
Table "public.tickets_identity"
Column | Type | Collation | Nullable | Default
---------+---------+-----------+----------+------------------------------
id | integer | | not null | generated always as identity
subject | text | | |
The serial column is an ordinary integer whose default calls nextval() on a sequence, so anything can overwrite it. The identity column is marked generated always as identity.
Which One to Use
- Use an identity column for new tables. It’s part of the SQL standard, and
GENERATED ALWAYSprotects your numbers. - Keep
serialonly in existing tables that already use it. - Either way, add a
PRIMARY KEY. An identity column doesn’t guarantee unique values on its own, as the documentation points out: a primary key does.
Clean Up
To remove the example, connect to another database and drop the test database:
\c postgres
You are now connected to database "postgres" as user "postgres".
DROP DATABASE helpdesk;
DROP DATABASE
Warning: DROP DATABASE deletes the database and everything in it, and it can’t be undone.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Typing your own IDs into a serial column | Nothing stops it, so two rows can share a number, as the two tickets numbered 1 showed. | Use GENERATED ALWAYS AS IDENTITY, and leave the column out of your inserts. |
| Expecting an identity column to stop every duplicate | It only picks the numbers; it doesn’t check them. | Add a PRIMARY KEY to the column. |
Troubleshooting
| Problem | Likely cause | Fix |
|---|---|---|
ERROR: cannot insert a non-DEFAULT value into column "id" | The column is GENERATED ALWAYS AS IDENTITY, and the insert gave it a value. | Leave the column out of the insert. If you really must set a number, for example when copying old data, add OVERRIDING SYSTEM VALUE after the column list, as the error’s hint says. |
FAQ
Can the numbers have gaps?
Yes, with both. The numbers come from a sequence, and a number taken by an insert that fails is never handed out again.
What’s the difference between ALWAYS and BY DEFAULT?
With ALWAYS, PostgreSQL refuses a number you type unless the insert says OVERRIDING SYSTEM VALUE. With BY DEFAULT, a number you type wins.
Conclusion
serial and identity columns both count up by themselves, but only GENERATED ALWAYS AS IDENTITY refuses a number typed by mistake. Use it for new tables, with a primary key to keep the numbers unique.
Additional Resources
- PostgreSQL 19 documentation: Identity Columns (opens in a new tab)
- PostgreSQL 19 documentation: Serial Types (opens in a new tab)
- PostgreSQL 19 documentation: CREATE TABLE (opens in a new tab)
Related
- PostgreSQL Tutorial — Part 5: Data Types Explained
- smallint vs integer vs bigint in PostgreSQL
- How to Generate a UUID in PostgreSQL
Postgres, PostgreSQL and the Slonik Logo are trademarks or registered trademarks of the PostgreSQL Community Association of Canada, and used with their permission.
0 comments