PostgreSQL – Add not-null Constraint

Below is the syntax to add the not null constraint to a table column in postgres database

ALTER TABLE table_name ALTER COLUMN column_name SET NOT NULL;

Above command will work only if no null values exist in the column. If the column has any null values, you need to first update all the null values to non-null values and then add the not-null constraint.

Not-null constraint can be dropped using below syntax:

ALTER TABLE table_name ALTER COLUMN column_name DROP NOT NULL;
See also  PostgreSQL - How to generate and insert a random uuid