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?
Enhance for @Nick and @mat suggestions: use
SELECT obj_description('schemaName.tableName'::regclass, 'pg_class');
when you have string name (not oid).To avoid to remember 'pg_class' parameter, and to avoid ugly concatenations at the function calls, as
(tname||'.'||schema)::regclass
, an useful overload forobj_description
:Now is easy to use, because the table name (
rname
parameter) is a varchar and can be expressed with a separated field for schema name, as in the main tables and queries.See also "Getting list of table comments in PostgreSQL" or the new pg9.3 Guide
This works for me using the PostBooks 3.2.2 DB:
Regards, Sylnsr
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:
You can then use the tableoid,columnoid tuple to extract the comment of each column (see my question).
This answer is a little late, but it popped up on a google search I did to research this problem. We only needed Table descriptions, but the method would be the same for columns. The column descriptions are in the pg_description table also, referenced by objoid.
Add this view:
Then run:
The view is a modified version of the pg_tables view which adds in the description column. You could also monkey around with the view definition to make it a single query.
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:
It fetches table and column descriptions, without any confusing joins and ugly string concatenations.