'Postgres Update column with Enum from string

I am building a migration. Changed a column from string to Enum-Type and want to insert the saved values as Enum into the table, but no idea how to do this. I inserted my values into a new table, droped the colum, created it with an enum type and got only the strings in the new table. The string have the same text as the enum. The code is:

UPDATE event_participations
    SET kind = participations_migration.kind
    FROM participations_migration
    WHERE participations_migration.part_id = event_participations.id

Participations_type is the name of the enum with values like "going", "not_going". Now I have strings in participations_migration.kind and want to get the enum value to update the event_participations.kind.

I need a subselect or join to update event_participations with the enum value according to the string which is set in participations_migration.kind. Any idea?



Solution 1:[1]

If the text is identical to the enum member text, a cast to the enum type should be enough.

    SET kind = participations_migration.kind::participations_type

Solution 2:[2]

A few years later - but I found the above doesn't work. Using postgres 13.5 and datagrip to query, you must add quotations around the enum like so:

SET kind = participations_migration.kind::"participations_type"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sticky bit
Solution 2 user37649