3

https://stackoverflow.com/a/771880/156458 provides a way to show all the tables defined in a database, via information schema. Since information schema is per database, I guess it won't be useful for showing all the databases in a cluster?

How would you show all the databases in a cluster in a comparable way, i.e. without using psql's command or option?

Thanks.

Tim
  • 1
  • 141
  • 372
  • 590

2 Answers2

5

Query the system catalog pg_database, e.g.:

select datname
from pg_database;

       datname       
---------------------
 template1
 template0
 postgres
 test
 db
 library_development
(6 rows)
klin
  • 112,967
  • 15
  • 204
  • 232
  • Thanks. Does SQL standard specify something relevant, and if yes, is your way close to the SQL standard? Does it have some portability to other RDBMS, up to some minor modification? – Tim Jun 15 '18 at 20:25
  • No, the term *database* is understood differently in various RDBMS. Also, the system catalogs are specific to Postgres. – klin Jun 15 '18 at 20:29
1

To show all postgres databases execute the command below on console: sudo su - postgres -c "psql -c '\l'"

Marcel Bezerra
  • 161
  • 1
  • 9