240

Postgresql got enum support some time ago.

CREATE TYPE myenum AS ENUM (
'value1',
'value2',
);

How do I get all values specified in the enum with a query?

1

5 Answers 5

454

If you want an array:

SELECT enum_range(NULL::myenum)

If you want a separate record for each item in the enum:

SELECT unnest(enum_range(NULL::myenum))  

Additional Information

This solution works as expected even if your enum is not in the default schema. For example, replace myenum with myschema.myenum.

The data type of the returned records in the above query will be myenum. Depending on what you are doing, you may need to cast to text. e.g.

SELECT unnest(enum_range(NULL::myenum))::text

If you want to specify the column name, you can append AS my_col_name.


Credit to Justin Ohms for pointing out some additional tips, which I incorporated into my answer.

Sign up to request clarification or add additional context in comments.

9 Comments

This answer is much more concise. Nice contribution!
The unnest call will return records of type myenum with a column name of "myenum". You might also want to cast the enum to text and specify a column name by adding something like. ::text AS my_column
what is the meaning of NULL::?
@ChrisL thanks. it seems very strange. why can't we do SELECT enum_range(myenum)? What is the meaning of casting null?
|
47

Try:

SELECT e.enumlabel
  FROM pg_enum e
  JOIN pg_type t ON e.enumtypid = t.oid
  WHERE t.typname = 'myenum'

3 Comments

If you have the same enum in more than one schema, this might need to be narrowed down a bit. If that's the case, see postgresql.org/docs/current/static/catalog-pg-type.html for details.
I think you need to prefix 'myenum' with an underscore. Check out my answer if you need to get enum values and the enum name may be used in more than one schema.
If enumeration order is important, append ORDER BY e.enumsortorder to the query. The enumerated values will most likely be out of order if new values were inserted into the enumeration type using BEFORE or AFTER.
16

In case someone is looking for the psql way then it's

\dT+ <your enum>

1 Comment

It starts with backslash, actually.
12
SELECT unnest(enum_range(NULL::your_enum))::text AS your_column

This will return a single column result set of the contents of the enum "your_enum" with a column named "your_column" of type text.

1 Comment

Casting to text allows you to sort the values
4

You can get all the enum values for an enum using the following query. The query lets you pick which namespace the enum lives in too (which is required if the enum is defined in multiple namespaces; otherwise you can omit that part of the query).

SELECT enumlabel
FROM pg_enum
WHERE enumtypid=(SELECT typelem
                 FROM pg_type
                 WHERE typname='_myenum' AND
                 typnamespace=(SELECT oid
                               FROM pg_namespace
                               WHERE nspname='myschema'))

2 Comments

What do you mean that it is the array type? This works for me (PostgreSQL 9.0).
+1 This worked for me whereas @Kev's answer did not, due to the use of schemas in my DB.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.