Database Postgres将列整数更改为布尔值

Database Postgres将列整数更改为布尔值,database,postgresql,Database,Postgresql,我有一个字段是INTEGER NOT NULL默认值0,我需要将其更改为bool 这就是我正在使用的: ALTER TABLE mytabe ALTER mycolumn TYPE bool USING CASE WHEN 0 THEN FALSE ELSE TRUE END; 但我得到了: ERROR: argument of CASE/WHEN must be type boolean, not type integer **

我有一个字段是INTEGER NOT NULL默认值0,我需要将其更改为bool

这就是我正在使用的:

ALTER TABLE mytabe 
ALTER mycolumn TYPE bool 
USING 
    CASE 
        WHEN 0 THEN FALSE 
        ELSE TRUE 
    END;
但我得到了:

ERROR:  argument of CASE/WHEN must be type boolean, not type integer

********** Error **********

ERROR: argument of CASE/WHEN must be type boolean, not type integer
SQL state: 42804
有什么想法吗

谢谢。

试试这个:

ALTER TABLE mytabe ALTER COLUMN mycolumn DROP DEFAULT;
ALTER TABLE mytabe ALTER mycolumn TYPE bool USING CASE WHEN mycolumn=0 THEN FALSE ELSE TRUE END;
ALTER TABLE mytabe ALTER COLUMN mycolumn SET DEFAULT FALSE;

您需要首先删除约束(因为它不是布尔值),其次您的
CASE
语句语法错误。

Postgres可以自动将整数转换为布尔值。关键短语是

using some_col_name::boolean
-- here some_col_name is the column you want to do type change
上面的答案是正确的,它只帮助我进行了一次修改,而不是我使用类型转换的情况

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;

公认的答案更好,因为它不依赖类型转换。此方法失败,例如,
无法将smallint类型转换为boolean
。@BenJohnson使用mycolumn::text::boolean尝试