PostgreSQL 17.12Lab-verified · 2026-12-01

PostgreSQL error 23502: null value in column violates not-null constraint

A NOT NULL column would get NULL — because the column was left out, the source value is NULL, or existing rows have no value.

PostgreSQL 17.12
23502: null value in column "email" of relation "members" violates not-null constraint

Jump to a cause

  1. 01The NOT NULL column is missing from the INSERT column list
  2. 02INSERT ... SELECT copies a NULL from the source table
  3. 03ADD COLUMN ... NOT NULL on a table that already has rows
  4. 04id column with no identity/serial (table moved from MySQL AUTO_INCREMENT, or created by hand)
  5. 05Sending NULL explicitly to a column that has a DEFAULT (common with ORMs)
  6. 06UPDATE ... SET col = (subquery) where some rows have no match
  7. 07ALTER COLUMN SET NOT NULL on a column that already has NULLs

Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-12-01 · container image postgres:17

23502 means a column declared NOT NULL would end up with NULL. The message names the column and table, and DETAIL: Failing row contains (...) shows the whole row that was rejected, with null in the problem position.

A column you do not list in INSERT gets its default, and with no default that is NULL. So a NOT NULL column without a default must always be given a value.

01The NOT NULL column is missing from the INSERT column list

INSERT INTO members (id, name) leaves out email, which is NOT NULL. The error names email, and Failing row contains (1, null, Kim) shows it got null.

The column check shows email has is_nullable = NO and no default. Listing email with a value makes the insert succeed.

Setup
Setuprun as lab_a
CREATE TABLE members (id int PRIMARY KEY, email text NOT NULL, name text);
OUTPUT
CREATE TABLE
Reproducerun as lab_a
INSERT INTO members (id, name) VALUES (1, 'Kim');
✕ ERROR
ERROR:  23502: null value in column "email" of relation "members" violates not-null constraint
DETAIL:  Failing row contains (1, null, Kim).
SCHEMA NAME:  lab_a
TABLE NAME:  members
COLUMN NAME:  email
Checkrun as lab_a
SELECT column_name, is_nullable, column_default FROM information_schema.columns WHERE table_schema = 'lab_a' AND table_name = 'members' ORDER BY ordinal_position;
OUTPUT
 column_name | is_nullable | column_default
-------------+-------------+----------------
 id          | NO          |
 email       | NO          |
 name        | YES         |
(3 rows)
After the fixrun as lab_a
INSERT INTO members (id, email, name) VALUES (1, 'kim@example.com', 'Kim') RETURNING id, email, name;
✓ SUCCESS
 id |      email      | name
----+-----------------+------
  1 | kim@example.com | Kim
(1 row)

INSERT 0 1

02INSERT ... SELECT copies a NULL from the source table

Copying from a staging table fails on the price column. Failing row contains (A-2, null) tells you which source row is the problem. The whole statement is rejected, so none of the three rows is inserted.

The check finds A-2 with an empty price. Here COALESCE(price, 0) fills it with 0 and all three rows go in (INSERT 0 3). Whether 0 is the right value depends on your data; you can also fix the source rows or filter them out.

Setup
Setuprun as lab_a
CREATE TABLE products (sku text PRIMARY KEY, price numeric NOT NULL);
CREATE TABLE staging_products (sku text, price numeric);
INSERT INTO staging_products VALUES ('A-1', 1000), ('A-2', NULL), ('A-3', 2500);
OUTPUT
CREATE TABLE
CREATE TABLE
INSERT 0 3
Reproducerun as lab_a
INSERT INTO products (sku, price) SELECT sku, price FROM staging_products;
✕ ERROR
ERROR:  23502: null value in column "price" of relation "products" violates not-null constraint
DETAIL:  Failing row contains (A-2, null).
SCHEMA NAME:  lab_a
TABLE NAME:  products
COLUMN NAME:  price
Checkrun as lab_a
SELECT sku, price FROM staging_products WHERE price IS NULL;
OUTPUT
 sku | price
-----+-------
 A-2 |
(1 row)
After the fixrun as lab_a
INSERT INTO products (sku, price) SELECT sku, COALESCE(price, 0) FROM staging_products;
SELECT sku, price FROM products ORDER BY sku;
✓ SUCCESS
INSERT 0 3
 sku | price
-----+-------
 A-1 |  1000
 A-2 |     0
 A-3 |  2500
(3 rows)

03ADD COLUMN ... NOT NULL on a table that already has rows

Adding status text NOT NULL to a table with rows fails with column "status" of relation "orders" contains null values. The new column would be NULL in every existing row.

The table has 2 rows. Adding the column with a default, ADD COLUMN status text NOT NULL DEFAULT 'new', fills existing rows with new and succeeds.

Setup
Setuprun as lab_a
CREATE TABLE orders (id int PRIMARY KEY, amount numeric);
INSERT INTO orders VALUES (1, 1200), (2, 500);
OUTPUT
CREATE TABLE
INSERT 0 2
Reproducerun as lab_a
ALTER TABLE orders ADD COLUMN status text NOT NULL;
✕ ERROR
ERROR:  23502: column "status" of relation "orders" contains null values
SCHEMA NAME:  lab_a
TABLE NAME:  orders
COLUMN NAME:  status
Checkrun as lab_a
SELECT count(*) AS existing_rows FROM orders;
OUTPUT
 existing_rows
---------------
             2
(1 row)
After the fixrun as lab_a
ALTER TABLE orders ADD COLUMN status text NOT NULL DEFAULT 'new';
SELECT id, amount, status FROM orders ORDER BY id;
✓ SUCCESS
ALTER TABLE
 id | amount | status
----+--------+--------
  1 |   1200 | new
  2 |    500 | new
(2 rows)

04id column with no identity/serial (table moved from MySQL AUTO_INCREMENT, or created by hand)

The INSERT leaves out id, expecting it to be numbered automatically. But id has no default, so it becomes NULL: Failing row contains (null, Third post).

Adding an identity to the existing column (ADD GENERATED BY DEFAULT AS IDENTITY (START WITH 3), after the existing ids 1 and 2) makes the same INSERT return id 3.

Setup
Setuprun as lab_a
CREATE TABLE posts (id int PRIMARY KEY, title text NOT NULL);
INSERT INTO posts VALUES (1, 'Hello'), (2, 'World');
OUTPUT
CREATE TABLE
INSERT 0 2
Reproducerun as lab_a
INSERT INTO posts (title) VALUES ('Third post');
✕ ERROR
ERROR:  23502: null value in column "id" of relation "posts" violates not-null constraint
DETAIL:  Failing row contains (null, Third post).
SCHEMA NAME:  lab_a
TABLE NAME:  posts
COLUMN NAME:  id
Check the causerun as lab_a
SELECT column_name, column_default, is_identity FROM information_schema.columns WHERE table_name = 'posts' AND column_name = 'id';
OUTPUT
 column_name | column_default | is_identity
-------------+----------------+-------------
 id          |                | NO
(1 row)

column_default is empty and is_identity is NO: nothing fills id automatically.

Fixrun as lab_a
ALTER TABLE posts ALTER COLUMN id ADD GENERATED BY DEFAULT AS IDENTITY (START WITH 3);
OUTPUT
ALTER TABLE
After the fixrun as lab_a
INSERT INTO posts (title) VALUES ('Third post') RETURNING id, title;
✓ SUCCESS
 id |   title
----+------------
  3 | Third post
(1 row)

INSERT 0 1

05Sending NULL explicitly to a column that has a DEFAULT (common with ORMs)

status has DEFAULT 'active', but the INSERT lists status with the value NULL. A DEFAULT is used only when the column is left out or DEFAULT is written; an explicit NULL is stored as NULL, so it fails with Failing row contains (1, kim@example.com, null).

With the keyword DEFAULT instead of NULL (or leaving status out of the list), the row gets active.

Setup
Setuprun as lab_a
CREATE TABLE users (id int PRIMARY KEY, email text NOT NULL, status text NOT NULL DEFAULT 'active');
OUTPUT
CREATE TABLE
Reproducerun as lab_a
INSERT INTO users (id, email, status) VALUES (1, 'kim@example.com', NULL);
✕ ERROR
ERROR:  23502: null value in column "status" of relation "users" violates not-null constraint
DETAIL:  Failing row contains (1, kim@example.com, null).
SCHEMA NAME:  lab_a
TABLE NAME:  users
COLUMN NAME:  status
Checkrun as lab_a
SELECT column_name, column_default, is_nullable FROM information_schema.columns WHERE table_name = 'users' AND column_name = 'status';
OUTPUT
 column_name | column_default | is_nullable
-------------+----------------+-------------
 status      | 'active'::text | NO
(1 row)

column_default is 'active'::text, but it is not used when NULL is given.

After the fixrun as lab_a
INSERT INTO users (id, email, status) VALUES (1, 'kim@example.com', DEFAULT) RETURNING id, email, status;
✓ SUCCESS
 id |      email      | status
----+-----------------+--------
  1 | kim@example.com | active
(1 row)

INSERT 0 1

06UPDATE ... SET col = (subquery) where some rows have no match

A scalar subquery that finds no row returns NULL. Park's dept_id 30 is not in depts, so dept_name becomes NULL and the whole UPDATE fails: Failing row contains (3, Park, 30, null).

With UPDATE ... FROM depts d WHERE d.id = e.dept_id, only matching rows are updated (UPDATE 2). Park keeps the old value (the default empty string) and can be fixed separately.

Setup
Setuprun as lab_a
CREATE TABLE depts (id int PRIMARY KEY, name text);
CREATE TABLE emps (id int PRIMARY KEY, name text, dept_id int, dept_name text NOT NULL DEFAULT '');
INSERT INTO depts VALUES (10, 'Sales'), (20, 'IT');
INSERT INTO emps (id, name, dept_id) VALUES (1, 'Kim', 10), (2, 'Lee', 20), (3, 'Park', 30);
OUTPUT
CREATE TABLE
CREATE TABLE
INSERT 0 2
INSERT 0 3
Reproducerun as lab_a
UPDATE emps e SET dept_name = (SELECT d.name FROM depts d WHERE d.id = e.dept_id);
✕ ERROR
ERROR:  23502: null value in column "dept_name" of relation "emps" violates not-null constraint
DETAIL:  Failing row contains (3, Park, 30, null).
SCHEMA NAME:  lab_a
TABLE NAME:  emps
COLUMN NAME:  dept_name
Check the causerun as lab_a
SELECT e.id, e.name, e.dept_id, d.name AS dept_name FROM emps e LEFT JOIN depts d ON d.id = e.dept_id ORDER BY e.id;
OUTPUT
 id | name | dept_id | dept_name
----+------+---------+-----------
  1 | Kim  |      10 | Sales
  2 | Lee  |      20 | IT
  3 | Park |      30 |
(3 rows)

The LEFT JOIN shows dept_name empty for Park (dept 30): the subquery returns no row for him.

Checkrun as lab_a
SELECT e.id FROM emps e WHERE NOT EXISTS (SELECT 1 FROM depts d WHERE d.id = e.dept_id);
OUTPUT
 id
----
  3
(1 row)

NOT EXISTS lists the rows without a department: only id 3.

After the fixrun as lab_a
UPDATE emps e SET dept_name = d.name FROM depts d WHERE d.id = e.dept_id;
SELECT id, name, dept_id, dept_name FROM emps ORDER BY id;
✓ SUCCESS
UPDATE 2
 id | name | dept_id | dept_name
----+------+---------+-----------
  1 | Kim  |      10 | Sales
  2 | Lee  |      20 | IT
  3 | Park |      30 |
(3 rows)

07ALTER COLUMN SET NOT NULL on a column that already has NULLs

The message is different here: column "phone" of relation "customers" contains null values. It is still 23502.

After filling the NULLs (UPDATE 2, here with unknown), SET NOT NULL succeeds.

Setup
Setuprun as lab_a
CREATE TABLE customers (id int PRIMARY KEY, name text, phone text);
INSERT INTO customers VALUES (1, 'Kim', '010-1111-2222'), (2, 'Lee', NULL), (3, 'Park', NULL);
OUTPUT
CREATE TABLE
INSERT 0 3
Reproducerun as lab_a
ALTER TABLE customers ALTER COLUMN phone SET NOT NULL;
✕ ERROR
ERROR:  23502: column "phone" of relation "customers" contains null values
SCHEMA NAME:  lab_a
TABLE NAME:  customers
COLUMN NAME:  phone
Check the causerun as lab_a
SELECT count(*) FILTER (WHERE phone IS NULL) AS null_rows, count(*) AS total FROM customers;
OUTPUT
 null_rows | total
-----------+-------
         2 |     3
(1 row)

2 of the 3 rows have phone NULL.

Fixrun as lab_a
UPDATE customers SET phone = 'unknown' WHERE phone IS NULL;
OUTPUT
UPDATE 2
After the fixrun as lab_a
ALTER TABLE customers ALTER COLUMN phone SET NOT NULL;
SELECT id, name, phone FROM customers ORDER BY id;
✓ SUCCESS
ALTER TABLE
 id | name |     phone
----+------+---------------
  1 | Kim  | 010-1111-2222
  2 | Lee  | unknown
  3 | Park | unknown
(3 rows)

FAQ

How do I fix null value in column violates not-null constraint?

Check the column named in the message and the Failing row contains line. Either give that column a value in the INSERT, replace NULLs from the source (for example with COALESCE), or set a default on the column.

Why does PostgreSQL insert NULL when I did not specify the column?

Columns left out of the INSERT list get their default. If there is no default, the value is NULL, which a NOT NULL column rejects.

How do I add a NOT NULL column to an existing table?

Add it with a default: ALTER TABLE t ADD COLUMN c text NOT NULL DEFAULT 'value';. Without a default the existing rows would be NULL and you get contains null values.

Why is id NULL when I insert without it in PostgreSQL?

The column has no identity or serial default. Check column_default and is_identity in information_schema.columns, then add one with ALTER COLUMN id ADD GENERATED BY DEFAULT AS IDENTITY.

Why is my DEFAULT value not used?

A DEFAULT is used only when the column is left out of the INSERT or written as DEFAULT. An explicit NULL (often sent by an ORM) stays NULL.

How we verify

Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.

lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12