Postgres DB Size Command

You can get the names of all the databases that you can connect to from the “pg_datbase” system table. Just apply the sql as below.

select pg.datname AS db_name, pg_size_pretty(pg_database_size(pg.datname)) as db_size
from pg_database pg
order by pg_database_size(pg.datname) desc;

If you intend the output to be consumed by a machine instead of a human, you can remove the pg_size_pretty() function.