Niki
Niki

Reputation: 49

how to add not null constraint for a column after dropping it if the table contains data

I have dropped the not null constraint for a column in postgresql. Now i need to add the not null constraint back to the column. My table has data in it already . How can i do it?

This column was created: columnname TEXT NOT NULL

Upvotes: 0

Views: 437

Answers (1)

arutar
arutar

Reputation: 1093

As described in Pg docs you can do something like this

update table_name set columnname='fill null data' where columnname is null;
ALTER TABLE table_name ALTER COLUMN columnname SET NOT NULL;

ALTER TABLE table_name ALTER COLUMN columnname SET default ' ';

Upvotes: 1

Related Questions