PostgreSQLERROR 23502

PostgreSQL: Null Value Violates Not-Null Constraint

PostgreSQL 23502 is raised when an INSERT or UPDATE attempts to store NULL in a column declared NOT NULL. The statement is aborted and no rows are changed.

Example Query

CREATE TABLE orders (id SERIAL PRIMARY KEY, user_id INT NOT NULL);
INSERT INTO orders (id) VALUES (1);
-- user_id is omitted, defaults to NULL

Common Causes

  1. 1Required column omitted from INSERT statement with no DEFAULT value
  2. 2Application passes NULL explicitly for a mandatory field
  3. 3Bulk import contains empty values for a NOT NULL column

How to Fix It

Supply a value for every NOT NULL column in your INSERT. Add a DEFAULT clause to the column definition if a sensible fallback exists. Validate data completeness before bulk imports.

Need a reliable database?

Try Supabase — free PostgreSQL with a generous free tier. No credit card required.

Get started free →

Got a query causing this error?

Paste it into SQLbuddy and get an instant plain-English explanation with optimization tips.

Analyze My Query →