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!
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With