Diagnostic Queries
Symptoms
A row being inserted or updated left a NOT NULL column empty. PostgreSQL names the column and table and shows the failing row in the DETAIL line.
- The write is rejected and the transaction rolls back with SQLSTATE 23502.
- The
DETAILline shows the failing row, withnullin the offending column. - Usually a column was omitted (with no default) or set to
NULLexplicitly.
What the server log shows
ERROR: null value in column "email" of relation "users" violates not-null constraint
DETAIL: Failing row contains (42, null, 2026-01-01).
STATEMENT: INSERT INTO users (id, created_at) VALUES (42, now());
Why PostgreSQL raises this — what the manual says
Section 5.5.2 Not-Null Constraints:
“A not-null constraint simply specifies that a column must not assume the null value.”
When a write produces a row whose NOT NULL column holds NULL — because the column was omitted and has no default, or was set to NULL explicitly — the constraint fails with 23502 and the whole statement rolls back.
Common causes
- Omitting a required column that has no
DEFAULT. - Inserting an explicit
NULLinto a required column. - An application sending an empty/missing field as
NULL. - An
UPDATEthat nulls out a required column.
How to fix it
- Provide a value for the column in every insert/update.
- Add a sensible
DEFAULT:ALTER TABLE users ALTER COLUMN created_at SET DEFAULT now();. - Fix the application mapping so the field is always populated.
- If the column should allow blanks, drop the constraint:
ALTER TABLE users ALTER COLUMN email DROP NOT NULL;.
Related & next steps
Reference: PostgreSQL 18 Section 5.4.2 “Not-Null Constraints”.
Thanks — noted. This helps keep the database accurate.