list Postgres ENUM type

前端 未结 9 2193
一个人的身影
一个人的身影 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.

    0 讨论(0)
  • 2020-12-12 12:22

    @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.

    0 讨论(0)
  • 2020-12-12 12:28

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

    0 讨论(0)
提交回复
热议问题