PostgreSQL Error 39004: Fix NULL Value Not Allowed
In this article
The PostgreSQL error 39004 null value not allowed appears when you try to insert or update a row where a column that is defined as NOT NULL receives a NULL value. In PostgreSQL, every column can be made mandatory by adding the NOT NULL constraint. The database then rejects any operation that would leave that column empty. This guide walks you through the exact steps to identify which column is causing the problem, fix the data or the constraint, and prevent the error from recurring.
What You'll Need
- Access to the PostgreSQL database where the error occurs (psql, pgAdmin, or any SQL client).
- Read/write permissions on the table you are modifying.
- Knowledge of the table name and the operation (INSERT or UPDATE) that failed.
Step-by-Step
- Find the exact error message. The full error usually includes the constraint name and the table, for example:
ERROR: null value in column "email" violates not-null constraint. If you only see the code 39004, run the failing query again and copy the complete message. The column name is almost always given. - Check the table definition. Run
\d table_namein psql or use a GUI to view the table structure. Look for columns markednot null. These are the ones that must have a value on every insert or update. - Identify the offending column. If the error message does not name the column, compare the values you are supplying with the
NOT NULLcolumns. For an INSERT, everyNOT NULLcolumn must appear in the column list (or have a default). For an UPDATE, you cannot set aNOT NULLcolumn to NULL. - Fix the data. Provide a valid value for the column. For example, if the column is
emailand you insertedNULL, change it to'user@example.com'. If the column should have a default, you can omit it from the INSERT list and let the default apply. - Alter the column to allow NULL (if appropriate). If the column should be optional, you can remove the
NOT NULLconstraint with:ALTER TABLE table_name ALTER COLUMN column_name DROP NOT NULL;. Be careful: this changes the schema and may affect application logic. - Add a DEFAULT value instead. If the column should never be NULL but you often forget to supply it, set a default:
ALTER TABLE table_name ALTER COLUMN column_name SET DEFAULT 'some_value';. Then inserts that omit the column will use the default. - Test the fix. Re-run the original INSERT or UPDATE. If it succeeds, the error is resolved. If not, read the new error message – it may point to another column or a different issue.
Common Mistakes
- Assuming the error is about the primary key. A primary key is automatically NOT NULL, but the error can be on any column. Always read the full message.
- Using an empty string instead of NULL. An empty string (
'') is not NULL. If the column accepts empty strings, that may work, but it is often not the intended fix. - Forgetting that UPDATE can trigger the error. The error is not only for INSERT. An UPDATE that sets a NOT NULL column to NULL will also fail.
- Dropping NOT NULL without checking application code. If your application relies on the column being mandatory, removing the constraint may cause silent data quality issues.
Tips
- Use
information_schema.columnsto find all NOT NULL columns programmatically:SELECT column_name FROM information_schema.columns WHERE table_name = 'your_table' AND is_nullable = 'NO'; - When writing INSERT statements, always list the columns explicitly:
INSERT INTO table (col1, col2) VALUES (1, 'a');– this makes it easier to see which columns are missing. - For bulk imports (COPY or pg_restore), the same error can appear. Check the source data for missing values before importing.
- If you are using an ORM (like Django, Rails, or Hibernate), the error may be masked. Look at the database logs for the exact SQL statement.
Troubleshooting
If the error persists after you think you have fixed it:
- Check for triggers. A BEFORE INSERT or BEFORE UPDATE trigger might be setting the column to NULL. Run
\d table_nameand look for triggers. Temporarily disable the trigger withALTER TABLE table_name DISABLE TRIGGER trigger_name;(re-enable after testing) to see if it is the cause. - Check for views or rules. If you are inserting into a view, the underlying table may have additional NOT NULL columns that the view does not expose. Insert into the base table instead.
- Verify the column is actually NOT NULL. Sometimes the error comes from a foreign key or a check constraint. The message will say “violates not-null constraint” – if it says something else, the problem is different.
- Look at the query plan. Run
EXPLAIN (VERBOSE) your_query;to see if any implicit casts or functions are producing NULL. - Restart the transaction. If you are in a transaction, a failed statement may have left the transaction in an aborted state. Use
ROLLBACK;and try again.
Frequently Asked Questions
Can I set a column to accept NULL without losing existing data?
Yes. Running ALTER TABLE ... DROP NOT NULL only changes the constraint; it does not modify existing rows. Any existing NULLs (if there were any) would remain, but if the column was NOT NULL, there are no NULLs to worry about.
Does this error happen only on INSERT?
No, it also happens on UPDATE if you set a NOT NULL column to NULL, and on COPY or any other data modification.
How do I find all NOT NULL columns in a table?
Use the query: SELECT column_name FROM information_schema.columns WHERE table_name = 'your_table' AND is_nullable = 'NO'; This lists all columns that do not allow NULLs.
What if I don't know which column is causing the error?
Run the failing query with VERBOSE or check the PostgreSQL log. The error message usually names the column. If not, compare your input values with the table definition.
Can I use a default value to avoid this error?
Yes, adding a DEFAULT to the column means that if you omit the column in an INSERT, PostgreSQL will use the default instead of NULL. This works for INSERT but not for UPDATE – if you explicitly set the column to NULL, the default is not applied.
Is it safe to drop the NOT NULL constraint?
It is safe from a database perspective, but consider your application logic. If the column is required by your application, dropping the constraint may allow invalid data. Always test thoroughly.