SQLSTATE 23502 ERROR Class 23: Integrity Constraint Violation

not_null_violation null value in column “…” of relation “…” violates not-null constraint — 23502

PostgreSQL error "null value in column "…" of relation "…" violates not-null constraint" (SQLSTATE 23502): what it means, common causes, and how to fix it.

PG 12, 13, 14, 15, 16, 17, 18 Official docs
Last reviewed May 2025 Grounded in source

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 DETAIL line shows the failing row, with null in the offending column.
  • Usually a column was omitted (with no default) or set to NULL explicitly.

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 NULL into a required column.
  • An application sending an empty/missing field as NULL.
  • An UPDATE that nulls out a required column.

How to fix it

  1. Provide a value for the column in every insert/update.
  2. Add a sensible DEFAULT: ALTER TABLE users ALTER COLUMN created_at SET DEFAULT now();.
  3. Fix the application mapping so the field is always populated.
  4. 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”.

Was this helpful?