PostgreSQL – Drop not-null Constraint

Below is the syntax to remove the not null constraint from a table in postgres database

ALTER TABLE table_name ALTER COLUMN column_name DROP NOT NULL;

Not-null constraint can be added to the existing column using below syntax:

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.

See also  PostgreSQL - List all databases