Modifying an existing table to use an ENUM instead of an int - Mailing list pgsql-general

From Mike Christensen
Subject Modifying an existing table to use an ENUM instead of an int
Date
Msg-id AANLkTimdgYrjWRhydTETZeImcZT1jW1TFQLEhht7tXKd@mail.gmail.com
Whole thread Raw
Responses Re: Modifying an existing table to use an ENUM instead of an int
List pgsql-general
Hi all -

I have an existing table that looks like this:

CREATE TABLE orders
(
  --Bunch of stuff you don't care about
  orderstate integer NOT NULL,
  --Etc
)

with a bunch of data in it.  I've now created this new data type:

CREATE TYPE OrderStateEnum AS ENUM ('Preview', 'InQueue', 'Ordered',
'Error', 'Cancelled');

I want to change the type of "orderstate" from integer to
OrderStateEnum, and cast 0 to Preview, 1 to InQueue, 2 to Ordered,
etc.

I could create a new column, copy all the data over, then delete the
old column, but I suspect there's some cool way to go about doing
this.  Thanks!

Mike

PS - This is totally shameless, but if you cook or your spouse does,
I'd totally appreciate it if you could take a short survey to help us
develop this website we're working on.  We're hoping to get about a
thousand responses so I have to plug it everywhere :)  This URL is
http://survey.kitchenpc.com/

pgsql-general by date:

Previous
From: Craig Ringer
Date:
Subject: Re: Error on Windows server could not open relation base/xxx/xxx Permission denied
Next
From: Mike Christensen
Date:
Subject: Re: Modifying an existing table to use an ENUM instead of an int