Sub topic 1
Creating and Using Enum Types in PostgreSQL
PostgreSQL enum types represent a fixed set of named values. An enum is useful when a column should accept one value from a small, stable list, such as an order status, a weekday, or a ticket priority.
This chapter assumes you know CREATE TABLE, INSERT, SELECT, WHERE, and basic PostgreSQL types. You should also understand that a database constraint rejects data that violates a rule.
Prerequisites
You should be comfortable with:
- Creating tables and columns
- Inserting and selecting rows
- Using
NOT NULLandDEFAULT - Understanding that a column has a data type
- Running SQL statements in PostgreSQL
Define an enum type
An enum type is a named PostgreSQL type whose allowed values are declared when the type is created. Each value is called an enum label.
The labels are written as strings, but they are not stored as ordinary text values. PostgreSQL checks that every value belongs to the enum type.
DROP TABLE IF EXISTS orders;
DROP TYPE IF EXISTS order_status;
CREATE TYPE order_status AS ENUM (
'pending',
'paid',
'shipped',
'delivered',
'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'),
('shipped');
INSERT INTO orders DEFAULT VALUES;
SELECT id, status
FROM orders
ORDER BY id;The CREATE TYPE statement creates order_status. The status column then uses that type instead of text.
The DEFAULT 'pending' rule applies when an insert does not provide a status. The statement INSERT INTO orders DEFAULT VALUES therefore creates a row with status pending.
The NOT NULL rule means every row must have a status. The column cannot contain SQL NULL.
What the type prevents
PostgreSQL rejects a label that was not declared in the enum.
INSERT INTO orders (status)
VALUES ('returned');This statement fails because returned is not one of the five labels in order_status. The database rejects the value before it stores the row.
An enum column also has a distinct type. PostgreSQL does not automatically treat every text expression as an order_status value. When a conversion is needed, cast explicitly:
SELECT 'paid'::order_status AS status;A cast tells PostgreSQL to interpret the string as a value of the enum type. The cast also checks that the label exists.
How enum values move through a table
The following diagram shows the path from a type definition to stored rows.
The enum defines the vocabulary. The column uses that vocabulary. Inserts can use only declared labels.
Use an enum in queries
You can compare an enum column with an enum literal and filter rows normally:
SELECT id, status
FROM orders
WHERE status = 'paid';
SELECT id, status
FROM orders
WHERE status IN ('paid', 'shipped');PostgreSQL infers the enum type for the string literals from the status column. You can also cast explicitly when the context is less clear:
SELECT id
FROM orders
WHERE status = 'paid'::order_status;Enum values are suitable for equality checks, IN lists, grouping, and ordering. They are less suitable when the allowed values change frequently or need extra data such as descriptions, display names, or sort weights. Those requirements may call for a separate table, which this tutorial discusses later.
Defaults and updates
A default is used only when an insert omits the column. It does not overwrite a value supplied by the application.
INSERT INTO orders (status)
VALUES ('delivered');
UPDATE orders
SET status = 'shipped'
WHERE id = 3;
SELECT id, status
FROM orders
ORDER BY id;The UPDATE statement can change the status only to another declared label. This protects the column even when different parts of an application write to the same database.
A default belongs to the table column, while the enum labels belong to the type. This distinction matters when one enum type is used by several tables. Changing a column's default changes that column's behavior, not the set of labels in the type.
When an enum is a good fit
Choose an enum when all of these conditions are mostly true:
- The values form a small, well-defined set.
- The values are part of the database model, not user-managed data.
- Invalid values should be rejected by the database.
- The labels are unlikely to be removed or changed often.
- The same value set may be useful across multiple columns or tables.
An enum gives you a reusable type and strong validation. It also documents the allowed values directly in the schema.
Before creating one, consider how the list will evolve. PostgreSQL supports adding and renaming enum values, but removing a value requires replacing the type. Later chapters cover those operations and their migration implications.
Exercise
What happens when INSERT INTO orders (status) VALUES ('returned'); runs against the order_status type defined in this chapter, and why?
Show answer
PostgreSQL rejects the insert because returned was not declared as a label in order_status. The enum type allows only pending, paid, shipped, delivered, and cancelled.