Displaying database size in PostgreSQL

I already have a note on checking database size in MySQL, here is the same for psql.

To find out the size of your databases, use this query:

SELECT datname AS "Data Base Name",
       pg_size_pretty(pg_database_size(datname)) AS "Data Base Size"
FROM pg_database
ORDER BY pg_database_size(datname) DESC;

Displaying table sizes within a database (connect to it first with \c db_name):

SELECT relname AS "Table",
       pg_size_pretty(pg_total_relation_size(relid)) AS "Size"
FROM pg_catalog.pg_statio_user_tables
ORDER BY pg_total_relation_size(relid) DESC;

Info about total disk space used across all postgres databases (run in bash):

psql -t -c "SELECT sum(pg_database_size(datname)) FROM pg_database;"

This returns bytes. Pipe it through numfmt if you want it human-readable:

psql -t -c "SELECT sum(pg_database_size(datname)) FROM pg_database;" | numfmt --to=iec

See also the full psql cheat sheet.