Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting list of table comments in PostgreSQL

Tags:

sql

postgresql

Postgresql allows adding comments to objects such as tables. For example I've added a comment to table "mytable" by using this SQL command:

COMMENT ON TABLE mytable IS 'This is my table.'; 

My question is: If I want to use a SQL-command to get all tables along with their respective comment - how would I do this? What would be the appropriate query for this?

Thanks in advance! Cheers!

like image 855
Bionicman303 Avatar asked Apr 14 '11 13:04

Bionicman303


People also ask

How do I see comments in PostgreSQL?

SELECT order_detail_id, quantity /* * Author: TechOnTheNet.com * Purpose: To show a comment that spans multiple lines in your SQL * statement in PostgreSQL. */ FROM order_details; This SQL comment spans across multiple lines in PostgreSQL - in this example, it spans across 5 lines.

How do I see table comments in SQL?

You can view the comments on a particular table or column by querying the data dictionary views USER_TAB_COMMENTS , DBA_TAB_COMMENTS , or ALL_TAB_COMMENTS or USER_COL_COMMENTS , DBA_COL_COMMENTS , or ALL_COL_COMMENTS . Specify the name of the operator to be commented.

How do I get a list of tables in PostgreSQL?

To list the tables in the current database, you can run the \dt command, in psql : If you want to perform an SQL query instead, run this: SELECT table_name FROM information_schema.

How do I see comments on Pgadmin?

To view the table comments select table in the tree control (on the left) and go to Properties tab in the browser (on the right). Comment is visible in Comment field.


1 Answers

All comments are stored in pg_description

To get the comments on a table, you need to join it to pg_class

As an alternative you can also use the function obj_description() to retrieve this information:

SELECT obj_description(oid) FROM pg_class WHERE relkind = 'r' 

Edit

In psql you can simply use the \d+ command to show all tables including their comments. Or use the \dd command to show all comments in the system

like image 187
a_horse_with_no_name Avatar answered Sep 16 '22 14:09

a_horse_with_no_name