Change column type and set not null

Postgresql

Postgresql Problem Overview


How do you change the column type and also set that column to not null together?

I am trying:

ALTER TABLE mytable ALTER COLUMN col TYPE character varying(15) SET NOT NULL

This returns an error.

What is the right syntax?

Postgresql Solutions


Solution 1 - Postgresql

This should be correct:

ALTER TABLE mytable
    ALTER COLUMN col TYPE character varying(15),
    ALTER COLUMN col SET NOT NULL

Solution 2 - Postgresql

Also, if you want to REMOVE NOT NULL constrain in postgresql:

ALTER TABLE mytable 
    ALTER COLUMN email DROP NOT NULL;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOto ShavadzeView Question on Stackoverflow
Solution 1 - PostgresqlFederico RazzoliView Answer on Stackoverflow
Solution 2 - PostgresqlShoniisraView Answer on Stackoverflow