Before Postgres insert data, it follow rules we defined: Constraints. There are many constraints in Postgres:
- CHECK constraints — to enforce data correctness
- Foreign Key constraints — to enforce integrity across tables
- UNIQUE constraints — to enforce uniqueness of every row
A very common use case is to use CHECK
constraint to enforce NOT NULL
.
Setting NOT NULL
on an existing column will block all reads and writes until every row is checked.
Add a CHECK constraint but not validate first.
Validate the constraint.
Then add the NOT NULL
.
Remove the CHECK constraint.