PostgreSQL Error 428C9: Generated Always Fix
In this article
PostgreSQL error 428C9 happens when you try to insert or update a value in a column defined as GENERATED ALWAYS AS IDENTITY or GENERATED ALWAYS AS (expression) STORED. The database refuses the write because the column is supposed to be filled automatically. The fix is to stop supplying that column in your INSERT or UPDATE statement, or, if you really must override it, use the OVERRIDING SYSTEM VALUE clause in the INSERT. For an update, you cannot change an identity column that is GENERATED ALWAYS; you must change the table definition or use a different column. This guide walks through the exact steps to diagnose and resolve the error, plus the common mistakes and troubleshooting tips.
What You'll Need
- Access to a PostgreSQL database (version 10 or later for identity columns; generated columns arrived in version 12).
- A SQL client such as psql, pgAdmin, DBeaver, or your application's database console.
- The ability to run
\d table_nameor queryinformation_schema.columnsto inspect column definitions. - Permission to alter the table if you need to change the column's generation attribute.
Step-by-Step
- Read the full error message. PostgreSQL will name the column and the table. For example:
ERROR: cannot insert a non-DEFAULT value into column "id"withDETAIL: Column "id" is an identity column defined as GENERATED ALWAYS.Note the column name; you will need it in the next steps. - Inspect the table definition. Run
\d your_tablein psql, or execute:SELECT column_name, is_identity, identity_generation, is_generated, generation_expression FROM information_schema.columns WHERE table_name = 'your_table';
Look foris_identity = YESwithidentity_generation = ALWAYS, oris_generated = ALWAYSfor a generated column. This confirms which column is protected. - Remove the column from your INSERT statement. The simplest fix: if your INSERT lists the identity column, delete it from the column list and the VALUES list. PostgreSQL will assign the next value automatically. For example, change
INSERT INTO t (id, name) VALUES (5, 'x');toINSERT INTO t (name) VALUES ('x');. - If you must supply a specific value, use OVERRIDING SYSTEM VALUE. For identity columns defined as GENERATED ALWAYS, PostgreSQL allows an explicit override with:
INSERT INTO t (id, name) OVERRIDING SYSTEM VALUE VALUES (5, 'x');
This bypasses the ALWAYS restriction for that statement only. Use it sparingly, for data migration or restoring rows with known IDs. - For UPDATE statements, do not touch the identity column. If your UPDATE sets the identity column, remove that assignment. If you need to change the value, you cannot do it directly on a GENERATED ALWAYS identity column; you would have to alter the column to GENERATED BY DEFAULT first (see next step) or use a different column for the changing value.
- If the column is a generated (computed) column, never write to it. Columns defined as
GENERATED ALWAYS AS (expression) STOREDare recalculated automatically. Remove them from INSERT and UPDATE statements entirely. If you need a different computed value, change the expression withALTER TABLE ... ALTER COLUMN ... SET EXPRESSION AS (...)(available in PostgreSQL 17+; in older versions you must drop and re-add the column, which loses data). - Consider changing the column to GENERATED BY DEFAULT if overrides are routine. If your application legitimately needs to insert explicit IDs sometimes, alter the column:
ALTER TABLE t ALTER COLUMN id SET GENERATED BY DEFAULT;
This allows explicit values without the OVERRIDING clause, while still auto-generating when omitted. Note that this changes behavior for all future inserts. - Fix your application code or ORM. Many frameworks include all columns in INSERT by default. Configure your model to mark the identity or generated column as read-only, or exclude it from insert statements. In SQLAlchemy, for example, use
server_defaultor set the column asautoincrement=Trueand avoid assigning it manually. - Test the corrected statement. Run your INSERT or UPDATE again in a transaction and roll back if needed. Verify the row was written with the expected values, especially if you used OVERRIDING SYSTEM VALUE.
Common Mistakes
- Using OVERRIDING SYSTEM VALUE on a generated (computed) column. That clause applies only to identity columns. Computed columns cannot be written at all; you must change the expression or drop the column.
- Forgetting that OVERRIDING SYSTEM VALUE is per-statement. It does not permanently change the column; every future explicit insert needs the clause unless you alter the column.
- Assuming GENERATED BY DEFAULT and GENERATED ALWAYS behave the same. BY DEFAULT allows explicit values without a clause; ALWAYS rejects them. Mixing them up leads to this error.
- Including the identity column in an ORM's bulk insert. Check the generated SQL, not just the model definition.
- Using a sequence's nextval() manually on an identity column. Identity columns manage their own sequence; calling nextval on the underlying sequence can cause conflicts.
Tips
- Use
GENERATED BY DEFAULT AS IDENTITYwhen you need flexibility for imports or migrations; useGENERATED ALWAYSwhen you want strict protection against accidental writes. - When restoring a dump that includes explicit identity values, pg_dump and pg_restore handle the OVERRIDING clause automatically for identity columns, but custom scripts may not.
- After inserting explicit values with OVERRIDING SYSTEM VALUE, the identity sequence is not advanced. If you later insert without a value, you may get a duplicate key error. Reset the sequence with
SELECT setval(pg_get_serial_sequence('t','id'), (SELECT MAX(id) FROM t));. - Check PostgreSQL version: identity columns require version 10+; generated columns require 12+. The error code 428C9 is the same, but available fixes differ.
Troubleshooting
If the error persists after removing the column from your statement, confirm you are editing the correct query. Search your codebase for the column name. If you are using a view or a rule, the underlying INSERT may still include the column; inspect the view definition with \d+ view_name. If you used OVERRIDING SYSTEM VALUE and still get an error, check that the column is indeed an identity column and not a generated column; the clause does not apply to the latter. If you altered the column to BY DEFAULT but the error remains, ensure the change was committed and that your connection is not using a cached prepared statement; reconnect or deallocate the statement. For permission errors masked as 428C9, verify you have INSERT privilege on the table. If you are unsure which column is at fault, run the failing statement with \set VERBOSITY verbose in psql to see the exact column and table names. Finally, if you cannot change the application code, consider a BEFORE INSERT trigger that strips the value, but this is a workaround, not a fix; altering the column or the query is preferable.
Frequently Asked Questions
What does PostgreSQL error 428C9 mean?
It means you tried to write an explicit value into a column defined as GENERATED ALWAYS (either an identity column or a generated/computed column). PostgreSQL blocks the write to protect the automatic value.
Can I insert a specific value into a GENERATED ALWAYS identity column?
Yes, by adding OVERRIDING SYSTEM VALUE to the INSERT statement. This is the only supported way to override the ALWAYS restriction for identity columns.
Why can't I use OVERRIDING SYSTEM VALUE on a generated column?
Generated columns (computed from an expression) are always calculated by the database and cannot be written at all. The OVERRIDING clause applies only to identity columns. To change a generated column's value, alter its expression or drop and re-add the column.
How do I change a GENERATED ALWAYS column to allow explicit values?
Run ALTER TABLE table_name ALTER COLUMN column_name SET GENERATED BY DEFAULT;. This changes the column so explicit values are accepted without the OVERRIDING clause.
Does this error affect UPDATE statements too?
Yes. Updating a GENERATED ALWAYS identity column or a generated column triggers the same error. Remove the column from the SET clause, or alter the column definition if you truly need to change it.
Will OVERRIDING SYSTEM VALUE advance the identity sequence?
No. The sequence is not updated when you supply an explicit value. If you insert a value larger than the current sequence, future automatic inserts may collide. Reset the sequence with setval after such inserts.