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?
Ok, so i worked it out to degree...
select col_description(table id, column number)...
ie: select col_description(36698,2);
That worked, but is there an easier way to do this maybe bringing all the comments on all the columns and using the table name instead of the oid???
I just found this here. It will provide you with all kind of metadata on one specific table (type, default value, not null flag, length, comment, foreign key name, primary key name). It seems to work well.
Source: http://golden13.blogspot.de/2012/08/how-to-get-some-information-about_7.html
I accessed table comments like this:
and column comments thusly:
Cool, that works to bring up a single comment, but is there a way to bring up all the comments from all the columns without multiple select statements or a loop?
And how do you launch this with a statement:
If you want to know which queries does psql run when you do \dt+ or \d+ customers, just launche it with -E.
Thanks
Take care with schemas, this code consider them:
References:
Postgresql Document Table and Column Description Comments on Table and Column
Determining the OID of a table in Postgres 9.1?
It all works by oid,
Now, I have the oid for that table, so I can ask :
Then, I can ask for the description of the fourth column :
If you want to know which queries does
psql
run when you do\dt+
or\d+ customers
, just run it with-E
.