Enumerated Types
An enum type restricts a column to a fixed, named set of values — you define the allowed values once, and PostgreSQL enforces that only those values can ever be stored. It's a clean way to model a genuinely fixed set of states or categories, like an order status or a mood.
Defining and using an enum
Creating an enum type
CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');
CREATE TABLE journal_entries (
entry_id SERIAL PRIMARY KEY,
entry_text TEXT NOT NULL,
entry_mood mood NOT NULL DEFAULT 'ok'
);
INSERT INTO journal_entries (entry_text, entry_mood)
VALUES ('Shipped the release today', 'happy');
INSERT INTO journal_entries (entry_text, entry_mood)
VALUES ('Bug report', 'grumpy');
-- error: invalid input value for enum mood: "grumpy"entry_id | entry_text | entry_mood
---------+------------------------------+------------
1 | Shipped the release today | happyWhy use an enum instead of a CHECK constraint?
You could enforce the same fixed value set with a TEXT column and a CHECK (entry_mood IN ('sad', 'ok', 'happy')) constraint, and it would work correctly. An enum type has two advantages over that approach: it's more storage-efficient (an enum value is stored internally as a small integer, not the full text of the label), and it's self-documenting — the valid values live in one named type that every column using it automatically shares, instead of being repeated inside a CHECK expression on each table.
Changing an enum's values later
Adding a new value
ALTER TYPE mood ADD VALUE 'ecstatic'; ALTER TYPE mood ADD VALUE 'meh' BEFORE 'ok';
CREATE TYPE name AS ENUM (...)defines a fixed, named set of allowed values.An enum column rejects any value outside that set, enforced by PostgreSQL itself.
Enums are more storage-efficient and more self-documenting than an equivalent
CHECK ... IN (...)constraint.ALTER TYPE ... ADD VALUEadds new values easily, but removing or reordering values is difficult and often requires recreating the type.For value sets that change frequently, a lookup table with a foreign key is usually the more flexible design.