Common SQL Errors

PostgreSQL Error 22004 – null_value_not_allowed Explained

August 4, 2025

PostgreSQL Error 22004 (null_value_not_allowed) appears when an INSERT or UPDATE attempts to put NULL into a column defined as NOT NULL or violates a domain constraint.

Sign up for the latest in common SQL errors from the Galaxy Team!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

What is PostgreSQL error 22004: null_value_not_allowed?

PostgreSQL Error 22004 – null_value_not_allowed occurs when a statement assigns NULL to a NOT NULL column or domain. Review the column definition, supply a non-NULL value, or drop the NOT NULL constraint to resolve the failure.

Error Highlights

Typical Error Message

PostgreSQL Error 22004

Error Type

Data Integrity Error

Language

PostgreSQL

Symbol

null_value_not_allowed

Error Code

22004

SQL State

Explanation

Table of Contents

What is PostgreSQL error 22004 (null_value_not_allowed)?

Error 22004 is a PostgreSQL data-integrity exception that fires when a query attempts to store a NULL in a target that forbids NULLs. The target can be a table column declared NOT NULL, a domain with a NOT NULL constraint, or a composite field inheriting such rules.

The server aborts the statement to protect data consistency.

Fixing the violation quickly is critical, because repeated failures block ETL jobs, application writes, and downstream analytics.

What Causes This Error?

INSERT or UPDATE statements that omit required columns trigger the exception when default values are not defined. ORMs often create partial column lists that silently pass NULL, causing runtime surprises.

Explicit NULL values supplied by application code will also raise error 22004 if the column is NOT NULL.

Bulk loads with COPY may fail when CSV fields are empty.

How to Fix PostgreSQL Error 22004

Identify the offending column in the error message. Provide a non-NULL literal or expression, use DEFAULT where appropriate, or relax the constraint.

If business logic requires NULLs, drop or alter the NOT NULL constraint.

Otherwise, backfill existing rows, then add NOT NULL and default clauses to prevent future NULLs.

Common Scenarios and Solutions

Missing timestamp columns in application INSERTs break older code after a schema change. Add the column to the insert list or define DEFAULT now().

COPY FROM CSV with empty strings fails when the target is NOT NULL.

Use CSV NULL handling (NULL "") or pre-clean the file.

Best Practices to Avoid This Error

Always declare sensible DEFAULT values for NOT NULL columns. Continuous integration tests should run INSERT/UPDATE suites after each migration.

Enable application-side validation and leverage Galaxy’s AI copilot to warn about NULL risk during query generation.

Related Errors and Solutions

Error 23502 (not_null_violation) is thrown when NULL violates a NOT NULL constraint at table level. Error 23514 (check_violation) fires when CHECK constraints fail.

Both are fixed by aligning input values with constraint rules.

.

Common Causes

Related Errors

FAQs

Does error 22004 only apply to tables?

No. Domains, composite types, and function arguments with NOT NULL also raise this error when they receive NULL.

Can I ignore the error and store NULL anyway?

PostgreSQL will not allow it unless you drop the NOT NULL constraint. Ignoring is impossible without schema change.

Why do I still get 22004 after setting a default?

Existing INSERT statements that list the column explicitly as NULL override the default. Remove the column from the column list or supply DEFAULT.

How does Galaxy help?

Galaxy’s schema-aware autocomplete highlights NOT NULL fields and suggests safe defaults, reducing the chance of runtime 22004 errors.

Start Querying with the Modern SQL Editor Today!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Check out some other errors

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo