2

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
  • 88,294
  • 128
  • 338
  • 543

2 Answers2

2

Query the system catalog pg_database, e.g.:

select datname
from pg_database;

       datname       
---------------------
 template1
 template0
 postgres
 test
 db
 library_development
(6 rows)
klin
  • 99,138
  • 12
  • 177
  • 203
  • 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'"