Retrieving Comments from a PostgreSQL DB

2019-01-18 02:12发布

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 couple of built in functions (pg_description and col_description) but i haven't been able to find examples on how to use them and playing around with them has proved pretty futile.

So I was wondering if any has been able to do this before and if so, how?

13条回答
Summer. ? 凉城
2楼-- · 2019-01-18 02:43

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';
查看更多
登录 后发表回答