Retrieving Comments from a PostgreSQL DB

后端 未结 13 2201
别那么骄傲
别那么骄傲 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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?

提交回复
热议问题