Helio Trigueiros
Helio Trigueiros

Reputation: 27

Migrating existing VARCHAR values to an Enum in Postgres 8

I have a table in production with values in a varchar column, but this column has now to be an enum. I've already created the enum, but how to migrate the existing values to the created Enum.

Will the database automatically map/migrate the existing values to the Enum by simply changing the column type?

Edit:

ALTER TABLE table ALTER COLUMN column TYPE new_enum_created;

I don't know if it might be useful but we use flyway for the migrations.

Upvotes: 1

Views: 866

Answers (1)

Laurenz Albe
Laurenz Albe

Reputation: 247950

You have to use a USING clause to tell PostgreSQL how to convert the data:

ALTER TABLE "table" ALTER COLUMN "column"
   TYPE new_enum_created USING "column"::new_enum_created;

Upvotes: 2

Related Questions