Retrieving Comments from a PostgreSQL DB

后端 未结 13 2032
别那么骄傲
别那么骄傲 2020-12-05 13:13

I\'m running a project on a Postgres database and need to retrieve the comments on columns within the DB to be used as table headings and such. I have seen that there are a

相关标签:
13条回答
  • 2020-12-05 13:44

    Just to be here if somebody will need it.

    There are many answers here, but none of them was as simple as I would like it to be. So, based on previous answers and current postgres 9.4, I have created this query:

    SELECT 
        obj_description(format('%s.%s',isc.table_schema,isc.table_name)::regclass::oid, 'pg_class') as table_description,
        pg_catalog.col_description(format('%s.%s',isc.table_schema,isc.table_name)::regclass::oid,isc.ordinal_position) as column_description
    FROM
        information_schema.columns isc
    

    It fetches table and column descriptions, without any confusing joins and ugly string concatenations.

    0 讨论(0)
  • 2020-12-05 13:44

    A slight change to one of the other answers which only gives you columns that have comments on them, this gives you all columns whether they have a comment or not.

    select c.table_schema, st.relname as TableName, c.column_name, 
    pgd.description
    from pg_catalog.pg_statio_all_tables as st
    inner join information_schema.columns c
    on c.table_schema = st.schemaname
    and c.table_name = st.relname
    left join pg_catalog.pg_description pgd
    on pgd.objoid=st.relid
    and pgd.objsubid=c.ordinal_position
    where st.relname = 'YourTableName';
    
    0 讨论(0)
  • 2020-12-05 13:45

    It all works by oid,

    mat=> SELECT c.oid FROM pg_catalog.pg_class c WHERE c.relname = 'customers';
      oid  
    -------
     23208
    (1 row)
    

    Now, I have the oid for that table, so I can ask :

    mat=> select pg_catalog.obj_description(23208);
      obj_description  
    -------------------
     Customers
    (1 row)
    

    Then, I can ask for the description of the fourth column :

    mat=> select pg_catalog.col_description(23208,4);
                 col_description             
    -----------------------------------------
     Customer codes, CHS, FACTPOST, POWER...
    (1 row)
    

    If you want to know which queries does psql run when you do \dt+ or \d+ customers, just run it with -E.

    0 讨论(0)
  • 2020-12-05 13:46

    This works for me using the PostBooks 3.2.2 DB:

    select cols.column_name,
    (select pg_catalog.obj_description(oid) from pg_catalog.pg_class c where c.relname=cols.table_name) as table_comment
    ,(select pg_catalog.col_description(oid,cols.ordinal_position::int) from pg_catalog.pg_class c where c.relname=cols.table_name) as column_comment
    from information_schema.columns cols
    where cols.table_catalog='postbooks' and cols.table_name='apapply'
    

    Regards, Sylnsr

    0 讨论(0)
  • 2020-12-05 13:49

    Take care with schemas, this code considers them:

    SELECT
        cols.column_name, (
            SELECT
                pg_catalog.col_description(c.oid, cols.ordinal_position::int)
            FROM
                pg_catalog.pg_class c
            WHERE
                c.oid = (SELECT ('"' || cols.table_name || '"')::regclass::oid)
                AND c.relname = cols.table_name
        ) AS column_comment
    FROM
        information_schema.columns cols
    WHERE
        cols.table_catalog    = 'your_database'
        AND cols.table_name   = 'your_table'
        AND cols.table_schema = 'your_schema';
    

    References:

    • Postgresql Document Table and Column Description Comments on Table and Column

    • Determining the OID of a table in Postgres 9.1?

    0 讨论(0)
  • 2020-12-05 13:49

    I asked a similar question about Postgresql comments last month. If you dig through that, you'll come across some Perl code over on my blog that automates the process of extracting a comment.

    To pull out the column names of a table, you can use something like the following:

    select
         a.attname  as "colname"
        ,a.attrelid as "tableoid"
        ,a.attnum   as "columnoid"
    from
        pg_catalog.pg_attribute a
        inner join pg_catalog.pg_class c on a.attrelid = c.oid
    where
            c.relname = 'mytable' -- better to use a placeholder
        and a.attnum > 0
        and a.attisdropped is false
        and pg_catalog.pg_table_is_visible(c.oid)
    order by a.attnum
    

    You can then use the tableoid,columnoid tuple to extract the comment of each column (see my question).

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