How to fix Null value not allowed in PostgreSQL
A NOT NULL constraint violation occurs when you try to insert or update a record with a NULL value in a column that requires a value. Learn how to identify the problematic column and fix the query.
Quick fix
Fixes Null value not allowed
ERROR: null value in column "username" violates not-null constraint- Identify the problematic column
- Check the table schema
- Update your INSERT statement
- Fix UPDATE statements with COALESCE
ERROR: null value in column "username" violates not-null constraintNull value not allowed
What this error means
In PostgreSQL, a NOT NULL constraint enforces that a column must always contain a value. When you attempt to insert or update a row without providing a value for a NOT NULL column, the database rejects the operation and returns this error. This constraint is a safeguard to ensure data integrity and prevent incomplete or invalid records from being stored in your database.
Typical symptoms
- INSERT or UPDATE queries fail with NOT NULL constraint error
- Application encounters database errors during data modification operations
- Specific column names are mentioned in the error message indicating which field is missing a value
- Transactions are rolled back entirely when this constraint is violated
Common causes
- Missing values in INSERT statements for NOT NULL columns
- Omitting a required column from an INSERT query
- Passing NULL explicitly or implicitly to a NOT NULL column in UPDATE statements
- Triggers or application logic that modifies data and introduces NULL values
- Stored procedures or functions that return NULL for required fields
- Data migration scripts that fail to provide values for newly added NOT NULL columns
How to fix "Null value not allowed"
The error message will tell you which column violates the NOT NULL constraint. Look for the column name in the error output:
ERROR: null value in column "username" violates not-null constraintIn this example, the "username" column is missing a value.
Query the table definition to see which columns have NOT NULL constraints:
\d your_table_nameOr use:
SELECT column_name, is_nullable, data_type
FROM information_schema.columns
WHERE table_name = 'your_table_name'
ORDER BY ordinal_position;Columns with is_nullable = false cannot accept NULL values.
Ensure your INSERT query includes all NOT NULL columns:
-- Wrong - missing "username" and "email"
INSERT INTO users (id, created_at)
VALUES (1, NOW());
-- Correct - all NOT NULL columns included
INSERT INTO users (id, username, email, created_at)
VALUES (1, 'john_doe', 'john@example.com', NOW());When updating data, use COALESCE to provide default values if NULL is encountered:
-- Before (may fail if column contains NULL)
UPDATE users
SET username = COALESCE(username, 'unknown')
WHERE id = 1;
-- Or use CASE for conditional logic
UPDATE users
SET username = CASE
WHEN username IS NULL THEN 'default_user'
ELSE username
END
WHERE id = 1;If you're adding a new NOT NULL column to an existing table with data, provide a default value:
-- Correct - provides default for existing rows
ALTER TABLE users
ADD COLUMN status VARCHAR(50) NOT NULL DEFAULT 'active';
-- Update any NULL values first if migrating
UPDATE users SET status = 'active' WHERE status IS NULL;
-- Then add the constraint
ALTER TABLE users
ADD CONSTRAINT status_not_null CHECK (status IS NOT NULL);If the error comes from your application:
// Wrong - undefined gets treated as NULL
const insertUser = async (user: any) => {
const result = await db.query(
'INSERT INTO users (id, username, email) VALUES ($1, $2, $3)',
[user.id, user.username, user.email] // email might be undefined
);
};
// Correct - validate before inserting
const insertUser = async (user: User) => {
if (!user.email) throw new Error('Email is required');
const result = await db.query(
'INSERT INTO users (id, username, email) VALUES ($1, $2, $3)',
[user.id, user.username, user.email]
);
};Advanced notes
If the error occurs in a transaction, the entire transaction is rolled back. Use savepoints to partially roll back failed operations without losing all work. For ORMs like Prisma or TypeORM, ensure your data models match the database schema and required fields are properly validated before database operations. When migrating databases or adding new columns, always test with sample data first to ensure constraints are satisfied.
Frequently asked questions
- What does "Null value not allowed" mean?
- In PostgreSQL, a NOT NULL constraint enforces that a column must always contain a value. When you attempt to insert or update a row without providing a value for a NOT NULL column, the database rejects the operation and returns this error. This constraint is a safeguard to ensure data integrity and prevent incomplete or invalid records from being stored in your database.
- How do I fix "Null value not allowed"?
- A NOT NULL constraint violation occurs when you try to insert or update a record with a NULL value in a column that requires a value. Learn how to identify the problematic column and fix the query.
- What causes "Null value not allowed"?
- Missing values in INSERT statements for NOT NULL columns Omitting a required column from an INSERT query Passing NULL explicitly or implicitly to a NOT NULL column in UPDATE statements
- Is the "Null value not allowed" error serious?
- Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.
Official resources & further reading
- ๐PostgreSQL Official Documentation
- ๐PostgreSQL Error Codes(Official Docs)