Updating Enum Values in PostgreSQL - The Safe and Easy Way

PHOTO EMBED

Thu Jan 12 2023 12:15:30 GMT+0000 (Coordinated Universal Time)

Saved by @jaimin #sql #postgresql

# remove references to the deprecated value
UPDATE job SET job_status = 'running' WHERE job_status = 'waiting';

# rename the existing type
ALTER TYPE status_enum RENAME TO status_enum_old;

# create the new type
CREATE TYPE status_enum AS ENUM('queued', 'running', 'done');

# update the columns to use the new type
ALTER TABLE job ALTER COLUMN job_status TYPE status_enum USING job_status::text::status_enum;
# if you get an error, see bottom of post

# remove the old type
DROP TYPE status_enum_old;
content_copyCOPY