środa, 13 czerwca 2012

How to obtain postgresql database size

If you want to find the size of a dataase , the simples method is use query : 

SELECT pg_database_size(‘databasename’);


postgres=# SELECT pg_database_size('aad');
 pg_database_size
------------------
       4582268508
(1 row)



For more human readable format please use:

SELECT pg_size_pretty(pg_database_size(‘databasename’));


postgres=# SELECT pg_size_pretty(pg_database_size('aad'));
 pg_size_pretty
----------------
 4370 MB
(1 row)

To get the size of all databases with alphabetical order:
 
SELECT datname,pg_size_pretty(pg_database_size(pg_database.datname)) FROM  pg_database ORDER BY datname;


postgres=# SELECT datname,pg_size_pretty(pg_database_size(pg_database.datname)) FROM  pg_database ORDER BY datname;
      datname      | pg_size_pretty
-------------------+----------------
 aad              | 4370 MB
 bae     | 4797 MB
 maad    | 534 MB
 pjvi            | 11 MB
 template0         | 4248 kB
 template1         | 4248 kB
 (6 rows)