223

The suggested query to list ENUM types is great. But, it merely lists of the schema and the typname. How do I list out the actual ENUM values? For example, in the linked answer above, I would want the following result

schema         type      values
-------------  --------  -------
communication  channels  'text_message','email','phone_call','broadcast'
1

10 Answers 10

253

You can list the data type via

\dT+ channels

https://www.postgresql.org/docs/current/static/app-psql.html#APP-PSQL-META-COMMANDS

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

1 Comment

@AlisonR. I believe the author was looking for an sql syntax. the solution in this answer is only good for the native postgres cli client.
244
select n.nspname as enum_schema,  
       t.typname as enum_name,  
       e.enumlabel as enum_value
from pg_type t 
   join pg_enum e on t.oid = e.enumtypid  
   join pg_catalog.pg_namespace n ON n.oid = t.typnamespace;

1 Comment

sweet... even better to use string_agg(e.enumlabel, ', ') as enum_value with the appropriate GROUP BYs. Many thanks.
196
select enum_range(null::my_enum);

where my_enum is the enum type name.

Documentation: http://www.postgresql.org/docs/9.5/static/functions-enum.html

1 Comment

Use select unnest(enum_range(null, null::name_of_enum_type)); to get one value per row.
43

This: SELECT unnest(enum_range(NULL::myenum)) returns enum types as rows.

Comments

22

I always forget how to do this. As per the other answer and the comment, here it is a comma-separated list. I like copy-paste snippets. Thanks for the help:

select n.nspname as enum_schema,  
    t.typname as enum_name,
    string_agg(e.enumlabel, ', ') as enum_value
from pg_type t 
    join pg_enum e on t.oid = e.enumtypid  
    join pg_catalog.pg_namespace n ON n.oid = t.typnamespace
group by enum_schema, enum_name;

Comments

8

SELECT enum_range(NULL::myenum)

1 Comment

Totally love this, shortest version ever. If you need to verify your data before casting it, you can run a row=any(SELECT enum_range(NULL::myenum)) either in the query or in WHERE clause and you'll see where you have issues.
0

@dpb:

If you want to create a permanent easy access method for this, you could always create a view

CREATE OR REPLACE VIEW oublic.enumz AS 
 SELECT n.nspname AS enum_schema,
  t.typname AS enum_name,
  e.enumlabel AS enum_value
 FROM pg_type t
 JOIN pg_enum e ON t.oid = e.enumtypid
 JOIN pg_namespace n ON n.oid = t.typnamespace;

You could then create a trigger for the insert command.

The above will store this in the database for future reference purposes.

Comments

0

This lists all enum-typed columns and their potential values:

SELECT
  table_schema || '.' || table_name || '.' || column_name as field_name,
  pg_enum.enumlabel as value
FROM pg_type
  JOIN pg_enum ON pg_enum.enumtypid = pg_type.oid
  JOIN pg_namespace on pg_type.typnamespace = pg_namespace.oid
  JOIN information_schema.columns ON (information_schema.columns.udt_name = pg_type.typname AND information_schema.columns.udt_schema = pg_namespace.nspname)
WHERE pg_type.typtype = 'e'
ORDER BY field_name, pg_enum.enumsortorder;

Comments

0

Add order

SELECT
  n.nspname AS enum_schema,
  t.typname AS enum_name,
  e.enumlabel AS enum_value
FROM
  pg_type t
  JOIN pg_enum e ON t.oid = e.enumtypid
  JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
ORDER BY
  enum_name,
  e.enumsortorder;

Comments

-2

If you have the table and column name, (but not the type name) use this:

SELECT pg_enum.enumlabel
FROM pg_type
 JOIN pg_enum ON pg_enum.enumtypid = pg_type.oid
 JOIN information_schema.columns ON information_schema.columns.udt_name =
                                    pg_type.typname
WHERE pg_type.typtype = 'e' AND
      table_name = $1 AND column_name = $2 ORDER BY pg_enum.enumsortorder

If you use enum_range on a column (in contrast to the other answers which used it on a type) it will return data for each row that exists, which is not what you want. So use the above query instead.

1 Comment

Since you don't join pg_namespace, this results in wrong associations if the same enum name is present in more than one schema...

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.