Sub topic 12
Enumerated types
PostgreSQL enumerated types, or enums, represent a fixed set of named values. This chapter assumes you know basic CREATE TYPE, CREATE TABLE, SELECT, and column constraints.
Define and use an enum
Create an enum with CREATE TYPE. Each label is a string, but PostgreSQL stores and validates it as a value of the enum type.
CREATE TYPE order_status AS ENUM (
'pending',
'paid',
'shipped',
'cancelled'
);
CREATE TABLE orders (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
status order_status NOT NULL DEFAULT 'pending'
);
INSERT INTO orders (status)
VALUES ('paid');
SELECT id, status
FROM orders
WHERE status = 'paid'::order_status;The cast is often optional when PostgreSQL can infer the expected type from the column or comparison. Writing 'paid'::order_status makes the type explicit.
An enum rejects values that are not in its definition:
INSERT INTO orders (status)
VALUES ('returned');
-- ERROR: invalid input value for enum order_status: "returned"This validation happens at the database boundary, so every application using the table follows the same allowed-value rule.
Ordering and comparisons
Enum labels have the order in which they were declared. The order is not alphabetical.
CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');
SELECT
'happy'::mood > 'ok'::mood AS is_happy_later,
'sad'::mood < 'ok'::mood AS is_sad_earlier;Both expressions return true. This makes comparisons and sorting useful when the labels have a real progression, such as workflow states. Do not use declaration order to represent priority unless that meaning is intentional.
flowchart TD
A[Create enum] --> B[Declare labels in order]
B --> C[Use enum as a column type]
C --> D[Database rejects unknown labels]
C --> E[Queries can compare labels]caption: How an enum controls allowed values alt: A flowchart showing enum creation, ordered labels, use in a table, validation, and comparisons.
When an enum fits
Enums work well when the set is small, stable, and part of the data model. They also make a column self-documenting.
| Choice | Best fit | Main trade-off |
|---|---|---|
| Enum | Small, stable set of states | Schema changes are required for new labels |
| Text with `CHECK` | Rules that may change often | The constraint must be maintained |
| Lookup table | Values have descriptions, ownership, or metadata | Requires a foreign key and another table |
Adding a label requires an ALTER TYPE statement:
ALTER TYPE order_status ADD VALUE 'returned' AFTER 'shipped';Enum labels can also be renamed, but removing a label or changing the declaration order is inconvenient. Those operations may require migrating data to another type. Because of this, an enum is usually a poor choice for values controlled by administrators or frequently changed by business users.
An enum is also different from a lookup table. It stores only the label and its internal enum identity. Use a lookup table when each value needs a display name, translation, active flag, sort position, or other attributes.
Exercise
You need a ticket_priority column with the stable values low, normal, and urgent, and urgent must sort after normal. What PostgreSQL definition would you use?
Show answer
CREATE TYPE ticket_priority AS ENUM ('low', 'normal', 'urgent'); Then use ticket_priority as the column type. The declaration order supplies the intended comparison order.