How to retrieve the comment of a PostgreSQL database?

postgresql

Solution

To get the comment on the database, use the following query:

select description from pg_shdescription
join pg_database on objoid = pg_database.oid
where datname = '<database name>'

This query will get you table comment for the given table name:

select description from pg_description
join pg_class on pg_description.objoid = pg_class.oid
where relname = '<your table name>'

If you use the same table name in different schemas, you need to modify it a bit:

select description from pg_description
join pg_class on pg_description.objoid = pg_class.oid
join pg_namespace on pg_class.relnamespace = pg_namespace.oid
where relname = '<table name>' and nspname='<schema name>'

Problem

I recently discovered you can attach a comment to all sort of objects in PostgreSQL. In particular, I'm interested on playing with the comment of a database. For example, to set the comment of a database: ``` COMMENT ON DATABASE mydatabase IS 'DB Comment'; ``` However, what is the opposite statement, to get the comment of `mydatabase`? From the `psql` command line, I can see the comment along with other information as a result of the `\l+` command; which I could use with the aid of awk in order to achieve my goal. But I'd rather use an SQL statement, if possible.

Original source