How to delete an enum type value in postgres?

前端 未结 9 2056
没有蜡笔的小新
没有蜡笔的小新 2020-12-04 08:02

How do I delete an enum type value that I created in postgresql?

create type admin_level1 as enum(\'classifier\', \'moderator\', \'god\');

9条回答
  •  南笙
    南笙 (楼主)
    2020-12-04 08:37

    For those who wish to modify the enum values, recreating it seems to be the only viable and safe solution.

    It consists in temporarely convert the enum column to a string format, recreate the enum and then reconverting the string column back to the enum type.

    Here is an example:

    ALTER TABLE your_schema.your_table ALTER COLUMN your_column TYPE varchar(255);
    ALTER TABLE your_schema.your_table ALTER COLUMN your_column SET DEFAULT('your_default_enum_value');
    DROP TYPE your_schema.your_enum_name;
    CREATE TYPE your_schema.your_enum_name AS ENUM ('enum1', 'enum2', 'enum3');
    ALTER TABLE your_schema.your_table ALTER your_column DROP DEFAULT;
    ALTER TABLE your_schema.your_table ALTER COLUMN your_column TYPE your_schema.your_enum_name USING your_enum_name::your_schema.your_column;
    ALTER TABLE your_schema.your_table ALTER COLUMN your_column SET DEFAULT('your_default_enum_value');
    

提交回复
热议问题