code sauce

...

Postgres Change Column Type From Boolean to Integer

| Comments

postgres change column type from boolean to integer

1
2
3
4
5
6
7
8
9
10
11
ALTER TABLE table_name ALTER column_name SET DEFAULT null;

ALTER TABLE table_name
ALTER column_name TYPE INTEGER
USING
CASE
  WHEN false THEN 0 ELSE 1
END;

ALTER TABLE table_name ALTER column_name SET DEFAULT 0;
COMMIT;

Comments