Postgres Alter Column Integer to Boolean

Viewed 42070

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 
USING 
    CASE 
        WHEN 0 THEN FALSE 
        ELSE TRUE 
    END;

But I am getting:

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

Any idea?

Thanks.

3 Answers

Also check that you don't have any CHECK constraint on you column like:

[...] CONSTRAINT blabla CHECK ((field = ANY (ARRAY[0, 1])))

otherwise you alter command will error with a cannot convert to boolean type

Related