Postgres Enum Cheat Sheet: Add, Rename, Remove, List Values

Mole mascot thinking, used as the cover for the enum cheat sheet.

Adding a label to a status column is a one-line change, and it is the change that gets people into trouble. The postgres enum add value statement is short. The transaction rule, the missing drop command, and the sort order are the parts that eat an afternoon.

Everything in the tables below was executed on PostgreSQL 16.15 unless the paragraph says it was not. Enum behaviour around transactions changed in older major versions. If you are on something earlier than 12, check that version’s ALTER TYPE page before you paste a migration into a runner that wraps every file in a transaction. The reference for 16 is ALTER TYPE and the enumerated types chapter.

Quick reference

You want SQL
Create the type CREATE TYPE order_status AS ENUM ('draft', 'paid', 'shipped');
Use it status order_status on the column, or a cast 'paid'::order_status
List labels, in order SELECT enum_range(NULL::order_status);
List every enum in the database Join pg_enum to pg_type (query below)
Add at the end ALTER TYPE order_status ADD VALUE 'refunded';
Add in the middle ALTER TYPE order_status ADD VALUE 'review' BEFORE 'paid';
Add only if missing ALTER TYPE order_status ADD VALUE IF NOT EXISTS 'refunded';
Rename a label ALTER TYPE order_status RENAME VALUE 'paid' TO 'complete';
Rename the type ALTER TYPE order_status RENAME TO order_state;
Remove a label No single command. Rebuild the type. Recipe below.
Sort by enum order ORDER BY status sorts by declaration order, not alphabetically

Labels are case sensitive and they are identifiers only in the sense that you quote them as strings. 'paid' and 'Paid' are different values.

Create an enum and use it in a table

CREATE TYPE order_status AS ENUM ('draft', 'paid', 'shipped');

CREATE TABLE orders (
  id int PRIMARY KEY,
  status order_status,
  note text
);

INSERT INTO orders (id, status, note) VALUES
  (1, 'draft', 'a'),
  (2, 'paid', NULL),
  (3, NULL, 'missing');

The third row is a real NULL. The column was declared without NOT NULL, so PostgreSQL stored it. If a status is required, say so on the column. The enum type itself has no null member.

Existing rows do not care that you created the type. They care when you change a column onto it. Adding a brand-new column with a default is the boring case:

ALTER TABLE orders
  ADD COLUMN channel order_status NOT NULL DEFAULT 'draft';

Changing a text column into an enum is the case that fails halfway through a migration. Every distinct value in the column has to be a label, or you supply a USING expression. The remove recipe further down is the same pattern.

Application code should pass the label as a bound parameter, not concatenate it into SQL. The cast error for a bad label is the database doing its job. Hiding that error in the app, by writing free text into a second column, is how two status systems appear.

List enum types and values

You will want the list before you add a label, because a typo creates a second label you then have to migrate away.

enum_range()

SELECT enum_range(NULL::order_status);
-- {draft,paid,shipped}

SELECT unnest(enum_range(NULL::order_status)) AS label;

enum_range with two arguments returns the slice between them, in enum order. enum_range('draft'::order_status, 'paid'::order_status) is draft then paid, not a string range. That is useful in a report and useless as a list of “everything”, because it stops at the second argument. Use the NULL::type form when you want all labels.

pg_enum join

This lists every enum in the current database, not just one type. enumsortorder is the order ORDER BY status uses.

SELECT n.nspname AS schema,
       t.typname AS enum_type,
       e.enumlabel,
       e.enumsortorder
FROM pg_enum e
JOIN pg_type t ON t.oid = e.enumtypid
JOIN pg_namespace n ON n.oid = t.typnamespace
WHERE n.nspname NOT IN ('pg_catalog', 'information_schema')
ORDER BY n.nspname, t.typname, e.enumsortorder;

On the test type this returned draft at sort order 1, paid at 2, shipped at 3. After ADD VALUE 'review' BEFORE 'paid', the range became draft, review, paid, shipped. Sort order is not “alphabetical”, and it is not “the order of the rows in the table”.

psql \dT+

\dT+ order_status in psql prints the type, the owner, the size (4 bytes), and the labels. It is the fastest check when you are already in a session. It is not SQL, so a migration cannot run it. Use enum_range in scripts.

If \dT+ says the type does not exist and you know it does, you are probably in the wrong database, or the type is in a schema that is not on your search_path. Schema-qualify it: \dT+ sales.order_status, or SELECT enum_range(NULL::sales.order_status).

Add a value

The postgres enum add value statement:

ALTER TYPE order_status ADD VALUE 'refunded';

To place a label next to one that already exists, name the neighbour. refunded is already on the type from the statement above, so this only adds review:

ALTER TYPE order_status ADD VALUE 'review' BEFORE 'paid';

That ran. The range afterwards was draft, review, paid, shipped, refunded. ALTER TYPE order_status ADD VALUE 'refunded' AFTER 'shipped' is how you would place refunded instead of the statement above. shipped was the last label, so AFTER 'shipped' lands in the same place. Run that instead of the first statement, not after it. A second ADD VALUE 'refunded' fails with enum label "refunded" already exists. BEFORE and AFTER refer to the enum order, not to alphabetical order and not to a row in your table. The neighbour has to be a current label. If you renamed paid to complete last week, AFTER 'paid' fails.

IF NOT EXISTS turns a repeat into a notice. This was run twice, once when paid was already there:

ALTER TYPE order_status ADD VALUE IF NOT EXISTS 'paid';
-- NOTICE: enum label "paid" already exists, skipping

Without IF NOT EXISTS, the same statement is an error: enum label "paid" already exists. Migrations that must be re-runnable want IF NOT EXISTS. Migrations that must fail when someone has already added a different meaning for that word should omit it.

Transactions

On PostgreSQL 16, ADD VALUE may run inside a transaction block. The new label cannot be used until that transaction commits. This is the note in the ALTER TYPE docs, and it is what the server did:

BEGIN;
ALTER TYPE order_status ADD VALUE 'archived';
INSERT INTO orders VALUES (4, 'archived', 'same tx');
-- ERROR: unsafe use of new value "archived" of enum type order_status
-- HINT: New enum values must be committed before they can be used.

That error aborts the transaction. The ADD VALUE rolls back with it. A later SELECT of that row returned nothing, and the label was not on the type. Two statements that each commit are the pattern that worked. Run them separately. A client that sends the block as one query runs it in one transaction, and the insert fails with unsafe use of new value "hold".

ALTER TYPE order_status ADD VALUE 'hold';
INSERT INTO orders (id, status, note) VALUES (9, 'hold', 'after commit');

Or an explicit transaction that only adds the label, a COMMIT, then the insert. Tools that wrap a whole migration file in one transaction will hit the error as soon as the file both adds a label and writes a row that uses it. Split the migration. Add the value in one release, start writing it in the next. That is also a kinder rollout: old application servers that do not know the new label will not receive it until you deploy the writer.

The docs also warn that a label added with BEFORE or AFTER can make later comparisons slightly slower, because the internal order no longer matches OID order. They describe the slowdown as usually insignificant. You do not need to rebuild the type to get correctness. Rebuild it only if you have measured a problem.

You must own the type to alter it. A migration role that can ALTER TABLE but does not own the enum will fail on ADD VALUE. ALTER TYPE order_status OWNER TO migration_role is the fix, run by the current owner.

Rename a value or the type

Renaming a label keeps its position. Existing rows are updated in the catalog sense: they store the enum OID, and the OID now displays as the new label. This ran:

ALTER TYPE order_status RENAME VALUE 'paid' TO 'complete';

The range became draft, review, complete, shipped, refunded, hold, because hold was added in the previous section. Rows that were paid read back as complete. Application code, check constraints written against the text, and analytics queries that say status = 'paid' do not update themselves. Search the codebase for the old label before you rename it.

Renaming the type does not change the labels:

ALTER TYPE order_status RENAME TO order_state;

Views, functions, and defaults that mention the old type name need a look. Columns that use the type follow the rename, because they store the type OID. A dump taken before the rename restores the old name. If you have a blue-green deploy, do not rename the type in the same minute the old app boots against the new database.

RENAME VALUE fails if the new label already exists or the old one does not. It does not accept IF NOT EXISTS.

Remove a value

There is no DROP VALUE that removes a label. On PostgreSQL 16.15 the server rejected the statement as a syntax error. On PostgreSQL 17 and 18 the statement parses and fails with dropping an enum value is not implemented.

ALTER TYPE order_state DROP VALUE 'draft';
-- PostgreSQL 16: ERROR: syntax error at or near "VALUE"
-- PostgreSQL 17 and 18: ERROR: dropping an enum value is not implemented

You build a new type and move the column. Run the setup below in this database. The rename above freed the name order_status, so the new type does not collide with order_state. The setup creates tickets, the open / closed / legacy type, the default, and the tickets_open index. legacy was mapped to closed. The NULL stayed NULL. open stayed open. The default was set again afterwards, and the index was created again on the new type.

CREATE TYPE order_status AS ENUM ('open', 'closed', 'legacy');
CREATE TABLE tickets (id int PRIMARY KEY, status order_status DEFAULT 'open');
CREATE INDEX tickets_open ON tickets (id) WHERE status = 'open';
INSERT INTO tickets VALUES (1, 'open'), (2, 'legacy'), (3, NULL);
BEGIN;

ALTER TABLE tickets ALTER COLUMN status DROP DEFAULT;
DROP INDEX tickets_open;

ALTER TYPE order_status RENAME TO order_status_old;

CREATE TYPE order_status AS ENUM ('open', 'closed');

ALTER TABLE tickets
  ALTER COLUMN status TYPE order_status
  USING (
    CASE status::text
      WHEN 'legacy' THEN 'closed'
      ELSE status::text
    END
  )::order_status;

ALTER TABLE tickets ALTER COLUMN status SET DEFAULT 'open';
CREATE INDEX tickets_open ON tickets (id) WHERE status = 'open';

DROP TYPE order_status_old;

COMMIT;

Result:

id status
1 open
2 closed
3 NULL

If any row holds a label the CASE does not map, the cast fails and the transaction rolls back. That is what you want. Add a SELECT status, count(*) FROM tickets GROUP BY status before the BEGIN so you know which labels exist. Do not run an UPDATE that rewrites production rows to hide a label and then discover the UPDATE was the destructive part.

The test table had DEFAULT 'open' and a partial index, tickets_open ON tickets (id) WHERE status = 'open'. Neither survived the type change on its own. Leaving the default in place failed with default for column "status" cannot be cast automatically to type order_status. Dropping the default and leaving the index failed with operator does not exist: order_status = order_status_old. Drop the default and that index before the type change, set the default again afterwards, and create the index again on the new type. ALTER TABLE shows the same pattern in its examples.

Do this in a maintenance window on a large table. Rewriting the column takes a lock and a new copy of the column’s values. ADD VALUE is the metadata change. Removing a value is a table rewrite. They are not the same kind of migration, even though both are “an enum change” in a ticket title.

Other schemas that use the same type have to move in the same transaction, or you cannot DROP TYPE. Search pg_type and the columns that depend on it before you start. DROP TYPE ... CASCADE will drop those columns. Do not use CASCADE to get the migration to go green.

Sort and compare by enum order

ORDER BY status uses enum order. After the labels were draft, paid, shipped, a select ordered by status returned draft, then paid, then the NULL. It did not return them alphabetically. The statements below continue on the original orders table, after the rename, so the type is order_state and the label that was paid is complete. They are not meant for tickets.

Comparisons use the same order:

SELECT 'shipped'::order_state > 'complete'::order_state;
-- t

That is handy for “everything up to and including the label that was paid”:

SELECT id
FROM orders
WHERE status <= 'complete';

It is a trap if someone later inserts a label in the middle. Adding a label before complete changes which rows this comparison returns:

-- ADD VALUE 'x' BEFORE 'complete'
SELECT id FROM orders WHERE status < 'complete';

If a report encodes “the pipeline” as enum order, treat BEFORE and AFTER as a behaviour change, not as a tidy-up.

Alphabetical sort, when you actually want it, has to go through text:

SELECT id FROM orders ORDER BY status::text;

Indexes on the enum column follow enum order. An index will not satisfy ORDER BY status::text. If you need both, you have two access paths, and the enum order is the one the type was built for.

Common errors

type “order_status” does not exist. The type is in another schema, or you are connected to another database. search_path does not include the schema. Qualify the name. In a migration, create the type in the same schema as the table that uses it, so a later restore does not depend on the path.

invalid input value for enum. 'Paid'::order_status failed with invalid input value for enum order_status: "Paid". The label was paid. Trim your inputs. A trailing space is also a different label. This is the error you get from a CSV import that has a status the type has never heard of. Fix the file or extend the type before you load it. Generating rows that only use real labels is covered in generate dummy data for Postgres.

unsafe use of new value. You added the label and used it in one transaction. Commit the ADD VALUE first. See the transaction section.

enum label already exists. You ran ADD VALUE without IF NOT EXISTS, or you are renaming onto a label that is already there.

must be owner of type. The role running the migration does not own the enum. Owners and superusers can alter it. Table owners are not automatically type owners.

An editor that knows the labels will not make these errors impossible. It does remove the class where you guessed the spelling. MoleDB’s SQL autocomplete suggests a column’s enum values as you type a predicate such as WHERE status = '. There is no recording of that on this page. The behaviour to expect is a suggestion of the real labels, which is the list enum_range would have printed. Whether that is the client you want overall is a separate question, covered with the other Mac apps in best Postgres client for Mac.

If you are deciding whether an enum is the right design at all, the trade is stability versus a label you will want to delete. Adding is easy. Removing rewrites the column. A lookup table is more work up front and less work the day a status goes away. That comparison deserves its own write-up. It is not required to use the statements on this page.

FAQ

How do I list all enum values in Postgres?

Use SELECT enum_range(NULL::your_enum); to get every label in sort order, or unnest that array for one label per row. For every enum in the database, join pg_enum to pg_type and order by enumsortorder. In psql, \dT+ your_enum prints the labels. On PostgreSQL 16 these three agreed with each other.

Can a Postgres enum be null?

The type cannot be null, but a column of that type can, unless you declared it NOT NULL. A test insert of a NULL status succeeded, and the row came back with an empty status. NULL sorts last in an ascending ORDER BY status, which is PostgreSQL's default NULLS LAST. A missing status and an unknown label are different: the unknown label is an error, the NULL is a stored value.

Are Postgres enums case sensitive?

Yes. Casting the text Paid to an enum whose label is paid failed with invalid input value for enum. Renames and comparisons use the label exactly as it was declared. If your application treats paid and Paid as the same status, the enum will not. Fix the writers, or store text with a CHECK constraint that rejects anything not lower-case, or normalise in a BEFORE trigger.

How do I remove a value from an enum?

You cannot drop one label in place. On PostgreSQL 16, ALTER TYPE ... DROP VALUE is a syntax error. On 17 and 18 it parses and fails with dropping an enum value is not implemented. Create a new enum without the label, alter the column with a USING expression that maps the old label to one you are keeping, and drop the old type. Do it in one transaction. Rows that already use the label you are deleting have to be mapped or the alter fails and the transaction rolls back.

Does Postgres support enums?

Yes. CREATE TYPE name AS ENUM (...) has been in PostgreSQL for many years. Labels are ordered by the sequence you add them, not by the alphabet. Each value occupies four bytes on disk. You can add a label, rename a label, and rename the type. You cannot drop a single label in place.

How does Postgres store enums?

A column value is the OID of a row in pg_enum, and pg_column_size of a label was 4 bytes on PostgreSQL 16.15. The text you see is a label in that catalog. Sort order is enumsortorder, which follows declaration order and any BEFORE or AFTER position you set when you added the label. Comparisons use that order, so shipped is greater than paid when shipped was declared later, even though that is not alphabetical.