ERROR: null value in column "..." of relation "..." violates not-null constraint means an INSERT, UPDATE or COPY tried to store NULL in a column declared NOT NULL. PostgreSQL rejects the row and the whole statement. The error is SQLSTATE 23502, not_null_violation. The constraint did its job. The real question is where the NULL came from.
INSERT INTO orders (customer_id, total) VALUES (NULL, 49.00);
ERROR: null value in column "customer_id" of relation "orders" violates not-null constraint
DETAIL: Failing row contains (1042, null, 49.00, 2026-11-24 15:12:03.21+00, new).
Read the message and the DETAIL line
The message names the column and, since PostgreSQL 13, the table as well. Older versions print only the column, which is confusing when a trigger or a rule writes into another table. The DETAIL: Failing row contains (...) line shows the whole row in column order, which helps you find which code path built it. A few details from the executor source:
- Each value is cut to 64 bytes and ends with
...if it is longer. - If you lack SELECT privilege on the table, only the columns you are allowed to see or that you wrote are shown.
- If row-level security is active on the table, the DETAIL line is left out completely.
Then check how the table is defined in the database the application actually uses:
SELECT column_name, is_nullable, column_default
FROM information_schema.columns
WHERE table_schema = 'public' AND table_name = 'orders'
ORDER BY ordinal_position;
Why the default did not save you
The most common surprise is that a column has a DEFAULT and the insert still fails. The INSERT documentation says that a column not present in the column list is filled with its default value, or with NULL if there is none. An explicit NULL is not "missing". It is a value, and PostgreSQL stores it, or rejects it:
-- the default is used: the column is not mentioned
INSERT INTO orders (customer_id, total) VALUES (7, 49.00);
-- the default is used: the DEFAULT keyword
INSERT INTO orders (customer_id, total, status) VALUES (7, 49.00, DEFAULT);
-- the default is NOT used: an explicit NULL, and the insert fails
INSERT INTO orders (customer_id, total, status) VALUES (7, 49.00, NULL);
ORMs and generic data layers often send every column of a model, with NULL for fields the code never set. In our experience this is the number one cause of this error right after a column gets NOT NULL DEFAULT ...: the database is ready, but the application still sends NULL explicitly. Either stop sending the column, send DEFAULT, or set the value in the application.
Other usual causes
- A subquery or join that finds nothing. A scalar subquery that returns no rows gives NULL, not an error (value expressions).
INSERT ... SELECTwith aLEFT JOINdoes the same for rows that have no match. - UPDATE with a lookup.
UPDATE orders SET customer_id = (SELECT id FROM customers WHERE email = ...)writes NULL wherever the lookup misses. - COPY and CSV files. In CSV format, the default NULL string is an unquoted empty value (COPY documentation). An empty field in a NOT NULL text column fails. If empty should mean an empty string, use
FORCE_NOT_NULLfor those columns. - A trigger. A
BEFOREtrigger that setsNEW.colto the result of a failed lookup produces the same error, even though the statement itself looked fine. - A migration on a table that already has rows. Adding a column with
NOT NULLand no default, or runningSET NOT NULLwhile old rows are NULL, fails with the related messagecolumn "..." of relation "..." contains null values. - Code and schema out of sync. A new column was added in one environment and the code was deployed to another. The same pattern causes the error in our article on column does not exist.
Do not "fix" it with DROP NOT NULL. Removing the constraint makes the error go away and moves the problem into your data: reports, joins and application code that assume the value exists. Find the code path that writes NULL and fix that, or give the column a real default.
Adding NOT NULL to a large table safely
On a small table, ALTER TABLE ... ALTER COLUMN ... SET NOT NULL finishes in milliseconds. On a table with hundreds of millions of rows, it is a different operation. The ALTER TABLE documentation explains that SET NOT NULL checks the whole table with a full scan, and like most forms of ALTER TABLE it holds an ACCESS EXCLUSIVE lock. Nothing can read or write the table during that scan. The same page describes the way around it: if a valid CHECK constraint already proves that no NULL can exist, the scan is skipped. That optimisation exists since PostgreSQL 12. VALIDATE CONSTRAINT needs only a SHARE UPDATE EXCLUSIVE lock, so normal reads and writes continue while it scans.
-- PostgreSQL 12 to 17: add NOT NULL to a big table without a long lock
SET lock_timeout = '3s';
-- 1. a CHECK constraint that only applies to new rows (short lock, no scan)
ALTER TABLE orders
ADD CONSTRAINT orders_customer_id_nn CHECK (customer_id IS NOT NULL) NOT VALID;
-- 2. fix the old rows in small batches
UPDATE orders SET customer_id = 0
WHERE id IN (SELECT id FROM orders WHERE customer_id IS NULL LIMIT 10000);
-- ...repeat until no rows are left...
-- 3. scan the table under SHARE UPDATE EXCLUSIVE: reads and writes keep working
ALTER TABLE orders VALIDATE CONSTRAINT orders_customer_id_nn;
-- 4. SET NOT NULL can now skip the full scan, then drop the helper constraint
ALTER TABLE orders ALTER COLUMN customer_id SET NOT NULL;
ALTER TABLE orders DROP CONSTRAINT orders_customer_id_nn;
PostgreSQL 18 stores NOT NULL constraints in pg_constraint and lets you add them as NOT VALID, so the helper CHECK constraint is no longer needed:
-- PostgreSQL 18: a NOT NULL constraint can itself be NOT VALID
ALTER TABLE orders
ADD CONSTRAINT orders_customer_id_nn NOT NULL customer_id NOT VALID;
-- ...backfill old rows...
ALTER TABLE orders VALIDATE CONSTRAINT orders_customer_id_nn;
For new columns, the cheap path is different. Since PostgreSQL 11, ADD COLUMN ... NOT NULL DEFAULT with a constant default does not rewrite the table. The value is stored in the catalog and returned for existing rows. A volatile default such as clock_timestamp() still rewrites the whole table.
What changes on large databases
- Waiting for the lock is as dangerous as holding it. Even the short
ADD CONSTRAINT ... NOT VALIDstep needs an exclusive lock. If a long query holds the table, the ALTER waits, and every new query queues behind it. Always setlock_timeoutand retry, as described in our guide to zero-downtime PostgreSQL migrations. - The backfill is the expensive part. Updating millions of rows writes new row versions, creates WAL and bloat, and can push replicas behind. In our experience, batches of a few thousand rows with pauses between them, plus watching replication lag, work better than one big
UPDATE. - Deploy order matters. First deploy code that always writes the value. Then backfill. Then add the constraint. If the constraint arrives before the code, every old application instance starts failing with 23502.
- Logical replicas have their own schema. If a subscriber table has NOT NULL on a column that the publisher sends as NULL, in our experience the apply worker stops on that row and retries until you fix it. Keep constraints in step on both sides.
- Error logs contain data. The DETAIL line puts row values, up to 64 bytes each, into the server log. If rows contain personal data, that data ends up in your log pipeline. Take this into account in log retention and access, and alert on a rising count of 23502 per table after each release.
Key takeaways
- 23502 means NULL reached a NOT NULL column. Read the column, table and failing row first.
- A DEFAULT applies only when the column is omitted or set to
DEFAULT. An explicit NULL is still NULL. - Look for empty lookups, LEFT JOINs, CSV empty fields and triggers.
- On big tables: CHECK ... NOT VALID, backfill in batches, VALIDATE, then SET NOT NULL. On PostgreSQL 18, use NOT NULL ... NOT VALID directly.
- Use
lock_timeout, deploy the code before the constraint, and watch replicas during the backfill.