list Postgres ENUM type

前端 未结 9 2192
一个人的身影
一个人的身影 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:01

    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;
    
    0 讨论(0)
  • 2020-12-12 12:02
    select enum_range(enum_first(null::province),null::province);
    
    0 讨论(0)
  • 2020-12-12 12:03

    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;
    
    0 讨论(0)
  • 2020-12-12 12:05

    You can list the data type via

    \dT+ channels
    

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

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

    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;
    
    0 讨论(0)
  • 2020-12-12 12:20
    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
    
    0 讨论(0)
提交回复
热议问题