Diagnostic Queries
Symptoms
An INSERT or UPDATE left a NOT NULL column without a value (or set it to NULL). PostgreSQL rejects the row with SQLSTATE 23502 (not_null_violation).
- The DETAIL shows the failing row, with NULL in the offending column.
- Common when a column has no default and the app omits it.
- Also appears when adding a NOT NULL column to a table with existing rows.
What the server log shows
ERROR: null value in column "email" of relation "users" violates not-null constraint
DETAIL: Failing row contains (17, null, …).
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.”
Before storing a row, PostgreSQL verifies every NOT NULL column holds a non-null value. A missing value with no default, or an explicit NULL, fails the check and the row is rejected with 23502.
Common causes
- The application did not supply a required column and there is no default.
- Inserting NULL explicitly into a required column.
- Adding a NOT NULL column without a default to a populated table.
How to fix it
- Provide a value for the column in the INSERT/UPDATE.
- Give the column a sensible
DEFAULTif appropriate. - When adding a NOT NULL column to existing data, add it with a default or backfill first, then set NOT NULL.
Related & next steps
Reference: PostgreSQL 18 Section 5.5 “Constraints”.
Thanks — noted. This helps keep the database accurate.