Postgres Alter Column Integer to Boolean

后端 未结 2 483
面向向阳花
面向向阳花 2021-01-31 07:22

I\'ve a field that is INTEGER NOT NULL DEFAULT 0 and I need to change that to bool.

This is what I am using:

ALTER TABLE mytabe 
ALTER mycolumn TYPE bool         


        
2条回答
  •  佛祖请我去吃肉
    2021-01-31 08:15

    Postgres can automatically cast integer to boolean. The key phrase is

    using some_col_name::boolean
    -- here some_col_name is the column you want to do type change
    

    Above Answer is correct that helped me Just one modification instead of case I used type casting

    ALTER TABLE mytabe ALTER COLUMN mycolumn DROP DEFAULT;
    ALTER TABLE mytabe ALTER mycolumn TYPE bool USING mycolumn::boolean;
    ALTER TABLE mytabe ALTER COLUMN mycolumn SET DEFAULT FALSE;
    

提交回复
热议问题