Retrieving Comments from a PostgreSQL DB

后端 未结 13 2158
别那么骄傲
别那么骄傲 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
    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).

提交回复
热议问题