← Back to PostgreSQL, page 1

postgresql-0002 · Easy fresher

Should a new table use SERIAL or an IDENTITY column for its primary key?

Use GENERATED ALWAYS AS IDENTITY (or BY DEFAULT) in new tables. It is SQL-standard, ties the sequence to the column, and with ALWAYS rejects manual ids unless OVERRIDING SYSTEM VALUE is given. SERIAL is older shorthand that creates a separate sequence and a default, with looser ownership and permission handling.

Full answer

Tables and rows used here:

sql
CREATE TABLE tickets (
  id      bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  subject text NOT NULL
);
CREATE TABLE legacy (
  id      serial PRIMARY KEY,
  subject text NOT NULL
);
sql
INSERT INTO tickets (subject) VALUES ('login fails'), ('slow page') RETURNING id, subject;

PostgreSQL returns:

text
 id |   subject
----+-------------
 1  | login fails
 2  | slow page
(2 rows)
sql
INSERT INTO tickets (id, subject) VALUES (100, 'manual id');

Result on PostgreSQL 17:

text
ERROR:  cannot insert a non-DEFAULT value into column "id"
sql
INSERT INTO tickets (id, subject) OVERRIDING SYSTEM VALUE VALUES (100, 'imported') RETURNING id;

Result:

text
  id
-----
 100
(1 row)
sql
INSERT INTO legacy (id, subject) VALUES (1, 'manual'); SELECT column_name, column_default, is_identity FROM information_schema.columns WHERE table_name IN ('tickets', 'legacy') AND column_name = 'id' ORDER BY table_name;

Output:

text
 column_name |           column_default           | is_identity
-------------+------------------------------------+-------------
 id          | nextval('legacy_id_seq'::regclass) | NO
 id          | NULL                               | YES
(2 rows)

The identity column numbers rows automatically. A manual id is rejected because the column is GENERATED ALWAYS, which protects against code accidentally bypassing the sequence; data imports can still use OVERRIDING SYSTEM VALUE. The serial column accepted a manual id silently, and its definition is just a default calling nextval, which is why information_schema shows it as not an identity. After importing explicit ids, move the sequence forward (ALTER TABLE ... ALTER COLUMN id RESTART WITH ...) so later inserts do not collide.

sql
CREATE TABLE tickets (
  id      bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  subject text NOT NULL
);
CREATE TABLE legacy (
  id      serial PRIMARY KEY,
  subject text NOT NULL
);

INSERT INTO tickets (subject) VALUES ('login fails'), ('slow page') RETURNING id, subject;

INSERT INTO tickets (id, subject) VALUES (100, 'manual id');

INSERT INTO tickets (id, subject) OVERRIDING SYSTEM VALUE VALUES (100, 'imported') RETURNING id;

INSERT INTO legacy (id, subject) VALUES (1, 'manual'); SELECT column_name, column_default, is_identity FROM information_schema.columns WHERE table_name IN ('tickets', 'legacy') AND column_name = 'id' ORDER BY table_name;

Run on PostgreSQL 17.12 (Debian 17.12-0+deb13u1) in a fresh schema, twice, with identical output.

Key points

  • Prefer IDENTITY in new tables
  • ALWAYS rejects manual values
  • OVERRIDING SYSTEM VALUE for imports
  • SERIAL is a default plus a sequence
  • Reset sequences after explicit-id imports

Common mistakes

  • Using serial in new designs
  • Importing ids without moving the sequence

Follow-ups

  • What is the difference in behaviour between ALWAYS and BY DEFAULT?
  • Why use bigint for ids?

Tips

  • Show the manual-id error