SQLSTATE 23502 ERROR Class 23: Integrity Constraint Violation

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

PostgreSQL error "null value in column "…" 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

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

  1. Provide a value for the column in the INSERT/UPDATE.
  2. Give the column a sensible DEFAULT if appropriate.
  3. 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”.

Was this helpful?