Altering the type of a column in postgres from one enum type to another requires an intermediate cast to text. Additionally, if there is a default value for the field, it cannot be altered and instead must be dropped and re-added as the new type.
Simple case: New type contains all old-type values
In the simple version, the new enum type contains (at least) all of the same labels as the old one. For instance, as with these two:
1 2 |
CREATE TYPE old_enum AS ENUM ('a', 'b', 'c', 'd'); CREATE TYPE new_enum AS ENUM ('a', 'b', 'c', 'd', 'e'); |
No default on column
If a table has an old_enum column and we want to turn it into a new_enum one, with no default value in place on the column, we can use the following command:
1 2 3 4 |
ALTER TABLE table_name ALTER COLUMN column_name SET DATA TYPE new_enum USING column_name::text::new_enum; |
The USING expression casts the current value of column_name to text, and then to new_enum. This works because every allowed value of the first enum type exists in the second.
With a default value
This case is not significantly more difficult to deal with. If there is a default value on the column, we simply remove it before altering the enum type of the column, and then add a new one when we’re done:
1 2 3 4 5 6 |
ALTER TABLE table_name ALTER COLUMN column_name DROP DEFAULT, ALTER COLUMN column_name SET DATA TYPE new_enum USING column_name::text::new_enum, ALTER COLUMN column_name SET DEFAULT 'a'; |
Converting enum labels
A more complicated scenario arises when not all of the old labels appear in the new enum. I’ll assume that there is a mapping from the old ones to the new, at least for every label that is known to appear in a row of the table. If there isn’t, then the conversion is probably not a good idea in the first place.
Consider now an even newer type,
1 |
CREATE TYPE newer_enum AS ENUM ('alpha', 'beta', 'c', 'd', 'e'); |
We still want to convert from the old_enum type, but now we also want to map the label ‘a’ to ‘alpha’, and ‘b’ to ‘beta’, while leaving ‘c’ and ‘d’ alone. This can be accomplished by manually applying each required change via a CASE statement in the USING expression:
1 2 3 4 5 6 7 8 9 10 11 12 |
ALTER TABLE table_name ALTER COLUMN column_name DROP DEFAULT, ALTER COLUMN column_name SET DATA TYPE newer_enum USING ( CASE column_name::text WHEN 'a' THEN 'alpha' WHEN 'b' THEN 'beta' ELSE column_name::text END CASE )::newer_enum, ALTER COLUMN column_name SET DEFAULT 'alpha'; |
For each record, this statement returns ‘alpha’ or ‘beta’ if the column contains ‘a’ or ‘b’, respectively, or otherwise returns the current value in the column cast as text. The returned value in all cases is then cast to the newer enum type.
Babe Ruth
Nice and thanks. How about dropping one of the old enum values (unused)?
michael
Do you mean that the old enum has labels (‘a’, ‘b’, ‘c’) and the new one just has (‘alpha’, ‘beta’), except there are no records that use label ‘c’? Or more simply that you have (‘a’, ‘b’, ‘c’) and want to turn it into (‘a’, ‘b’) because nothing is using ‘c’?
Yansen
I think you forgot to put “END” on when using CASE.
Yansen
Great tutorial anyway, it helps me a lot
munderwood
Indeed, thanks! I’ve updated the post, hopefully no more syntax errors…
Muthamizhselvi
how to drop enum values
Marin Aldimriov
@Muthamizhselvi
https://stackoverflow.com/questions/1771543/adding-a-new-value-to-an-existing-enum-type
Alex
Thanks, this was great. Only s/end case/case/ :)
Alex
Sorry I meant, s/end case/end/ :P
Craig
Very helpful. Thanks!
Thiago Pereira
Great Thanks!!!
themsay
Great! Thanks for your time.
Rich
Another Thank you from the future ;)