PostgreSQL ENUM type
To create enum type
CREATE TYPE colors AS ENUM (‘grey’, ‘skyblue’, ‘black’);
To list enum values
SELECT ENUM_RANGE(null::colors); or
SELECT UNNEST(ENUM_RANGE(null::colors)) AS colors;
To add new item on the enum list
ALTER TYPE colors ADD VALUE 'orange' AFTER 'skyblue';
To delete item from enum list
ALTER TYPE colors DROP attribute 'orange';
To drop enum type
DROP type colors
There is a more elegant way using domain to enforce value from David E. Wheeler post
CREATE DOMAIN eye_color AS TEXT
CONSTRAINT valid_eye_colors CHECK (
VALUE IN ( 'blue', 'green', 'brown' )
);
CREATE TABLE faces (
face_id SERIAL PRIMARY KEY,
name TEXT NOT NULL DEFAULT '',
eye_color eye_color NOT NULL
);
Related
Enum support function
Alter type
Enforce set of values