Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-30 · container image postgres:17
A column declared GENERATED ALWAYS AS IDENTITY takes its numbers only from its sequence, and a GENERATED ALWAYS AS (...) STORED column only from its expression. Writing your own value to either gives SQLSTATE 428C9. The DETAIL line tells you which kind of column it is.
Below are four common cases: an INSERT with id, copying rows with old ids, changing an id, and inserting into a calculated column.
01INSERT with a value for a GENERATED ALWAYS identity column
orders.id is GENERATED ALWAYS AS IDENTITY. INSERT INTO orders (id, amount) VALUES (2, 2000) failed; DETAIL says Column "id" is an identity column defined as GENERATED ALWAYS.
Leaving id out of the column list worked. PostgreSQL gave the new row id = 2.
Setup
CREATE TABLE orders (id int GENERATED ALWAYS AS IDENTITY PRIMARY KEY, amount int);
INSERT INTO orders (amount) VALUES (1000);CREATE TABLE
INSERT 0 1INSERT INTO orders (id, amount) VALUES (2, 2000);ERROR: 428C9: 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.SELECT column_name, is_identity, identity_generation FROM information_schema.columns WHERE table_name = 'orders' AND column_name = 'id'; column_name | is_identity | identity_generation
-------------+-------------+---------------------
id | YES | ALWAYS
(1 row)is_identity = YES and identity_generation = ALWAYS: the column does not accept your values. BY DEFAULT would.
INSERT INTO orders (amount) VALUES (2000);INSERT 0 1SELECT id, amount FROM orders ORDER BY id; id | amount
----+--------
1 | 1000
2 | 2000
(2 rows)02Copying rows with their old ids into a new identity table
Moving data with INSERT INTO orders SELECT id, amount FROM orders_old failed, because the old ids (101, 102) are explicit values. The HINT says Use OVERRIDING SYSTEM VALUE to override.
INSERT INTO orders OVERRIDING SYSTEM VALUE SELECT ... copied both rows. The identity sequence does not move when you override it, so we set it to the highest id with setval(pg_get_serial_sequence('orders', 'id'), (SELECT max(id) FROM orders)). The next normal INSERT got id = 103.
Setup
CREATE TABLE orders_old (id int PRIMARY KEY, amount int);
INSERT INTO orders_old VALUES (101, 1000), (102, 2000);
CREATE TABLE orders (id int GENERATED ALWAYS AS IDENTITY PRIMARY KEY, amount int);CREATE TABLE
INSERT 0 2
CREATE TABLEINSERT INTO orders SELECT id, amount FROM orders_old;ERROR: 428C9: 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.INSERT INTO orders OVERRIDING SYSTEM VALUE SELECT id, amount FROM orders_old;
SELECT setval(pg_get_serial_sequence('orders', 'id'), (SELECT max(id) FROM orders));INSERT 0 2
setval
--------
102
(1 row)INSERT INTO orders (amount) VALUES (3000);
SELECT id, amount FROM orders ORDER BY id;INSERT 0 1
id | amount
-----+--------
101 | 1000
102 | 2000
103 | 3000
(3 rows)03UPDATE of a GENERATED ALWAYS identity column
UPDATE orders SET id = 10 WHERE id = 1 failed with column "id" can only be updated to DEFAULT.
If the ids really must be editable, ALTER TABLE orders ALTER COLUMN id SET GENERATED BY DEFAULT changes the column. After that the same UPDATE worked (UPDATE 1), and the row became 10 | 1000.
Setup
CREATE TABLE orders (id int GENERATED ALWAYS AS IDENTITY PRIMARY KEY, amount int);
INSERT INTO orders (amount) VALUES (1000), (2000);CREATE TABLE
INSERT 0 2UPDATE orders SET id = 10 WHERE id = 1;ERROR: 428C9: column "id" can only be updated to DEFAULT
DETAIL: Column "id" is an identity column defined as GENERATED ALWAYS.ALTER TABLE orders ALTER COLUMN id SET GENERATED BY DEFAULT;ALTER TABLEUPDATE orders SET id = 10 WHERE id = 1;
SELECT id, amount FROM orders ORDER BY id;UPDATE 1
id | amount
----+--------
2 | 2000
10 | 1000
(2 rows)04INSERT into a generated (calculated) column
total is GENERATED ALWAYS AS (price * qty) STORED. Giving it a value in the INSERT failed; DETAIL says Column "total" is a generated column.
Inserting only id, price and qty worked, and total was calculated as 1500.
Setup
CREATE TABLE order_items (id int PRIMARY KEY, price int, qty int,
total int GENERATED ALWAYS AS (price * qty) STORED);CREATE TABLEINSERT INTO order_items (id, price, qty, total) VALUES (1, 500, 3, 1500);ERROR: 428C9: cannot insert a non-DEFAULT value into column "total"
DETAIL: Column "total" is a generated column.INSERT INTO order_items (id, price, qty) VALUES (1, 500, 3);INSERT 0 1SELECT id, price, qty, total FROM order_items; id | price | qty | total
----+-------+-----+-------
1 | 500 | 3 | 1500
(1 row)FAQ
How do I insert my own id into a GENERATED ALWAYS identity column?
Add OVERRIDING SYSTEM VALUE after the table name. The sequence does not move, so we also ran setval to the max id; the next normal INSERT then got 103.
What is the difference between GENERATED ALWAYS and BY DEFAULT?
ALWAYS rejects your values (428C9). After ALTER COLUMN id SET GENERATED BY DEFAULT, the UPDATE that failed here worked.
Why does INSERT ... SELECT fail when copying a table?
The SELECT passes the old ids into the identity column. Use OVERRIDING SYSTEM VALUE, or leave the id out of both lists so new ids are generated.
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12