How do I delete an enum type value that I created in postgresql?
create type admin_level1 as enum('classifier', 'moderator', 'god');
E.g. I want to remove moderator
from the list.
I can't seem to find anything on the docs.
I'm using Postgresql 9.3.4.
The programmatic way to do this is as follows. The same general steps as given in https://stackoverflow.com/a/47305844/629272 are appropriate, but those are more manual than made sense for my purposes (writing an alembic down migration).
my_type
,my_type_old
, andvalue_to_delete
, should, of course, be changed as appropriate.Rename your type.
Create a new type with the values from your old type, excluding the one you want to delete.
Change all existing columns which use the old type to use the new one.
Delete the old type.
if your dataset is not so big you can dump with
--column-inserts
edit the dump with a text editor, remove the value and re-import the dumpFor 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:
You delete (drop) enum types like any other type, with
DROP TYPE
:Is it possible you're actually asking about how to remove an individual value from an enum type? If so, you can't. It's not supported:
You must create a new type without the value, convert all existing uses of the old type to use the new type, then drop the old type.
E.g.
Very well written here:
http://blog.yo1.dog/updating-enum-values-in-postgresql-the-safe-and-easy-way/
rename the existing type
create the new type
update the columns to use the new type
remove the old type
Use following query to Delete ENUM value from Postgresql type
Just info for what's type and what's value
You should change existing values to other. For that if you need to add new value, then use:
Before deleting, update type value to new type value or existing value.