Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL, get column name, column type and description

Tags:

postgresql

I want to for selected table in selected schema get desired result like this

column_name  |  data_type   | description
-----------------------------------------
id           | integer      |  id of bill
name         | character    |
address      | character    | adress of buyer

notice that some columns don't have description (column comment).

For now i got only this query which gives me good result but only for columns which got comments (for columns that are in the selected table and don't have comment are not represented in output).

MY query which returns only data for columns that have comment is next

SELECT c.column_name, c.data_type, pgd.description 
from pg_catalog.pg_statio_all_tables as st 
inner join pg_catalog.pg_description pgd on (pgd.objoid=st.relid) 
inner join information_schema.columns c on (pgd.objsubid=c.ordinal_position and  c.table_schema=st.schemaname and c.table_name=st.relname)  
where table_schema = 'public' and table_name = 'some_table';

How to get columns without comment in result?

like image 689
KuKeC Avatar asked Jun 12 '17 06:06

KuKeC


1 Answers

There is the function col_description(table_oid, column_number)

select column_name, data_type, col_description('public.my_table'::regclass, ordinal_position)
from information_schema.columns
where table_schema = 'public' and table_name = 'my_table';

 column_name | data_type |  col_description  
-------------+-----------+-------------------
 gid         | integer   | a comment for gid
 id          | integer   | 
 max_height  | numeric   | 
(3 rows)
like image 74
klin Avatar answered Sep 28 '22 20:09

klin