list Postgres ENUM type

前端 未结 9 2198
一个人的身影
一个人的身影 2020-12-12 11:57

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

9条回答
  •  隐瞒了意图╮
    2020-12-12 12:20

    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.

提交回复
热议问题