Search code examples
postgresqlenumscasting

Is it possible to cast from one enum to another in PostgreSQL


I need to populate a new table in a second schema from an existing one, but having problems casting the "schema1.a.disclosure_level" column enum to the "schema2.b.disclosure_level" enum. A cast via ::text or :: varchar did not help. Casting to ::schema1.a.disclosure_level raises a cross-database reference error.

INSERT INTO schema1.a (id, disclosure_level)
SELECT schema2.b.id, schema2.b.disclosure_level
FROM schema2.b;

Any ideas?


Solution

  • @Bergi showed me the solution.

    INSERT INTO schema1.a (id, disclosure_level)
    SELECT schema2.b.id, schema2.b.disclosure_level::text:schema1.disclosure_level_enum
    FROM schema2.b;
    

    where my fault was to use the column name instead of the enum type definition in the cast: schema1.disclosure_level_enum (type) instead of schema1.a.disclosure_level (column)!