PostgreSQL Error 23502: Fix Not Null Violation
In this article
PostgreSQL error 23502 is a not-null violation: an INSERT or UPDATE tried to write a NULL into a column defined as NOT NULL. The fastest fix is to find which column is missing a value, then either supply a value in the statement or add a DEFAULT to the column so future inserts work without it. The error message names the column, for example null value in column "email" of relation "users" violates not-null constraint, so read that line first — it tells you exactly where to look.
What You'll Need
- Access to the database with a role that can read the table definition and run the failing statement (or the application logs that show it).
- A PostgreSQL client:
psql, pgAdmin, or your application's database tool. - The exact failing SQL statement or the ORM code that generated it — the error alone does not show which values were sent.
- Permission to change the table schema (ALTER TABLE) if the real fix is a default value or a relaxed constraint.
Step-by-Step
- Read the full error text. PostgreSQL reports the column name, the table (relation) name, and the constraint name. If you only see a generic 23502 in logs, enable full error logging or reproduce the statement manually so you get the detail line.
- Inspect the table definition. Run
\d+ your_tablein psql, or queryinformation_schema.columnsfiltering onis_nullable = 'NO'. This lists every NOT NULL column, which is the candidate set. Note which ones have no default. - Compare the failing statement to the column list. An INSERT that omits a NOT NULL column without a default will fail. An UPDATE that sets a column to NULL, or a JOIN that produces NULL, will also fail. Check the actual values bound by your application, including empty strings converted to NULL by some drivers.
- Decide the correct fix. There are three legitimate options: (a) supply a real value in the statement; (b) add a DEFAULT to the column so omitted inserts succeed; (c) drop the NOT NULL constraint only if NULL is genuinely valid business data. Do not drop the constraint just to silence the error.
- Apply the value fix. For example:
INSERT INTO users (email, name) VALUES ('a@example.com', 'Ana');— make sure every NOT NULL column without a default appears in the column list. - Apply the default fix if appropriate. For example:
ALTER TABLE users ALTER COLUMN created_at SET DEFAULT now();This changes future inserts only; it does not backfill existing rows. - Backfill existing NULLs before adding a constraint. If you are tightening a column, first run
UPDATE your_table SET col = 'fallback' WHERE col IS NULL;, verify the count, thenALTER TABLE your_table ALTER COLUMN col SET NOT NULL;. - Re-run the original statement or application flow. Confirm the insert or update now succeeds, and check that no trigger or rule is silently setting the column to NULL afterwards.
- Fix the source, not just the symptom. Update the ORM model, form validation, or API schema so the field is required at the application layer as well. Otherwise the same error returns on the next code path.
Common Mistakes
- Dropping NOT NULL to make the error go away. This hides a data-quality bug and lets invalid rows in. Only remove the constraint if NULL is a valid state for that column.
- Assuming a default exists. A column can be NOT NULL with no default; omitting it from an INSERT still fails. Check the definition before assuming.
- Forgetting triggers. A BEFORE INSERT trigger can overwrite a supplied value with NULL, producing 23502 even though your statement included the column.
- Confusing empty string with NULL. Some drivers and ORMs convert empty form fields to NULL. Validate and normalise input before it reaches the database.
- Adding NOT NULL to a populated table without a backfill. The ALTER will fail on existing NULL rows; clean them first.
- Ignoring the column name in the error. The message already tells you which column failed — start there instead of guessing.
Tips
- Use
INSERT ... ON CONFLICTonly for uniqueness conflicts, not for NOT NULL violations — it does not solve a missing value. - Give audit columns such as
created_ata default likenow()so application code cannot forget them. - In development, set
log_min_error_statementand check the PostgreSQL log to capture the exact statement and parameters. - When using an ORM, enable SQL logging temporarily to see the generated INSERT and which columns it omits.
- Keep a migration that adds a default and a migration that backfills as separate steps so each can be verified.
Troubleshooting
If the error persists after adding the value, check for a trigger or rule on the table that modifies the row. Query pg_trigger for the table, or inspect the trigger function body. If the error appears only from the application and not from psql, log the bound parameters — a driver may be sending NULL for an empty field. If an ALTER TABLE ... SET NOT NULL fails, run a count of NULLs first and backfill them. If the column name in the error does not exist in your model, a migration may be out of date; compare the live schema with your migration files. If you lack permission to alter the table, ask the database owner rather than working around it in application code. If the error happens inside a function or stored procedure, check the variable assigned to the column — an unset variable is NULL.
Frequently Asked Questions
What does PostgreSQL error 23502 mean exactly?
It is the SQLSTATE for a not-null violation: a statement attempted to store NULL in a column declared NOT NULL.
How do I find which column caused it?
The error detail line names the column and relation, for example null value in column "email" of relation "users". If your logs hide it, reproduce the statement in psql.
Can I set a default value instead of changing every insert?
Yes. ALTER TABLE ... ALTER COLUMN ... SET DEFAULT ... makes omitted inserts succeed, but only for future rows; existing NULLs are unaffected.
Why does the error happen only sometimes?
Usually because one code path supplies the column and another omits it, or because a form field is empty and the driver converts it to NULL.
Does ON CONFLICT fix error 23502?
No. ON CONFLICT handles unique and exclusion constraint conflicts, not missing required values.
Is it safe to drop the NOT NULL constraint?
Only if NULL is a valid state for that column in your data model. Otherwise fix the data source and keep the constraint.