Skip to main content
0 reactions

Identity vs Serial in PostgreSQL: Which to Use for IDs

Featured card for Identity vs Serial in PostgreSQL, comparing identity (SQL standard, GENERATED ALWAYS, refuses typed IDs) with serial (older shorthand, a sequence default, allows duplicates).
Identity vs serial in PostgreSQL.

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

SyntaxSQL
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 an integer column with a default taken from a sequence, a counter object PostgreSQL creates for it.
  • Use bigint instead of integer for a table that could pass 2 billion rows; bigserial is 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:

postgres=#SQL
CREATE DATABASE helpdesk;
OutputPlain text
CREATE DATABASE
postgres=#Plain text
\c helpdesk
OutputPlain text
You are now connected to database "helpdesk" as user "postgres".

Step 2: Create Both Tables

helpdesk=#SQL
CREATE TABLE tickets_serial (id serial, subject text);
OutputPlain text
CREATE TABLE
helpdesk=#SQL
CREATE TABLE tickets_identity (id integer GENERATED ALWAYS AS IDENTITY, subject text);
OutputPlain text
CREATE TABLE

Step 3: Add a Ticket to Each

Leave out the id, and PostgreSQL fills it in:

helpdesk=#SQL
INSERT INTO tickets_serial (subject) VALUES ('Printer is jammed');
OutputPlain text
INSERT 0 1
helpdesk=#SQL
INSERT INTO tickets_identity (subject) VALUES ('Printer is jammed');
OutputPlain text
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:

helpdesk=#SQL
INSERT INTO tickets_serial (id, subject) VALUES (1, 'Screen is blank');
OutputPlain text
INSERT 0 1

The identity table refuses it:

helpdesk=#SQL
INSERT INTO tickets_identity (id, subject) VALUES (1, 'Screen is blank');
OutputPlain text
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:

helpdesk=#SQL
SELECT * FROM tickets_serial;
OutputPlain text
 id |      subject
----+-------------------
  1 | Printer is jammed
  1 | Screen is blank
(2 rows)
helpdesk=#SQL
SELECT * FROM tickets_identity;
OutputPlain text
 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:

helpdesk=#Plain text
\d tickets_serial
OutputPlain text
                             Table "public.tickets_serial"
 Column  |  Type   | Collation | Nullable |                  Default
---------+---------+-----------+----------+--------------------------------------------
 id      | integer |           | not null | nextval('tickets_serial_id_seq'::regclass)
 subject | text    |           |          |
helpdesk=#Plain text
\d tickets_identity
OutputPlain text
                     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 ALWAYS protects your numbers.
  • Keep serial only 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:

helpdesk=#Plain text
\c postgres
OutputPlain text
You are now connected to database "postgres" as user "postgres".
postgres=#SQL
DROP DATABASE helpdesk;
OutputPlain text
DROP DATABASE

Warning: DROP DATABASE deletes the database and everything in it, and it can’t be undone.

Common Mistakes

MistakeWhat happensDo this instead
Typing your own IDs into a serial columnNothing 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 duplicateIt only picks the numbers; it doesn’t check them.Add a PRIMARY KEY to the column.

Troubleshooting

ProblemLikely causeFix
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

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