Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

Is there any command to find all the databases size in Postgres?

I am able to find the size of a specific database by using following command:

select pg_database_size('databaseName');
share|improve this question

You can get the names of all the databases that you can connect to from the "pg_datbase" system table. Just apply the function to the names, as below.

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

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

share|improve this answer
    
Sometimes database contains indexes also. It has some storage value. I am looking for one command that will provide size of the complete database. – user2151087 Sep 20 '13 at 4:12
3  
@user2151087: pg_database_size() includes the sizes for indexes – a_horse_with_no_name Sep 20 '13 at 6:00

And... If in case you may not want to type a whole query... you can also type...

\l+ <database_name>

and you will get some details about the database, including the size of the database.

And... To get sizes of all databases.

you can just type...

\l+

You may need to go into the postgresql command prompt to query with these postgresql helper commands.

Check other postgresql helper commands by typing

\?

at the postgresql command prompt.

share|improve this answer
4  
Most concise answer. helpful to boot. – Jerome Aug 11 '14 at 9:18
2  
beautiful answer! love this command! – sufinawaz Feb 5 '15 at 21:10
    
This should be the accepted answer – kid_drew Feb 11 at 22:19
-- Database Size
SELECT pg_size_pretty(pg_database_size('Database Name'));
-- Table Size
SELECT pg_size_pretty(pg_relation_size('table_name'));
share|improve this answer
2  
How is that answer different to Mike's? – a_horse_with_no_name Sep 20 '13 at 6:58

Based on the answer here by @Hendy Irawan

Show database sizes:

\l+

e.g.

=> \l+
 berbatik_prd_commerce    | berbatik_prd     | UTF8     | en_US.UTF-8 | en_US.UTF-8 |                       | 19 MB   | pg_default | 
 berbatik_stg_commerce    | berbatik_stg     | UTF8     | en_US.UTF-8 | en_US.UTF-8 |                       | 8633 kB | pg_default | 
 bursasajadah_prd         | bursasajadah_prd | UTF8     | en_US.UTF-8 | en_US.UTF-8 |                       | 1122 MB | pg_default | 

Show table sizes:

\d+

e.g.

=> \d+
 public | tuneeca_prd | table | tomcat | 8192 bytes | 
 public | tuneeca_stg | table | tomcat | 1464 kB    | 

Only works in psql.

share|improve this answer

From the PostgreSQL wiki.


Databases to which the user cannot connect are sorted as if they were infinite size.

SELECT d.datname AS Name,  pg_catalog.pg_get_userbyid(d.datdba) AS Owner,
    CASE WHEN pg_catalog.has_database_privilege(d.datname, 'CONNECT')
        THEN pg_catalog.pg_size_pretty(pg_catalog.pg_database_size(d.datname))
        ELSE 'No Access'
    END AS Size
FROM pg_catalog.pg_database d
    ORDER BY
    CASE WHEN pg_catalog.has_database_privilege(d.datname, 'CONNECT')
        THEN pg_catalog.pg_database_size(d.datname)
        ELSE NULL
    END DESC -- nulls first
    LIMIT 20

The page also has snippets for finding the size of your biggest relations and largest tables.

share|improve this answer

Start pgAdmin, connect to the server, click on the database name, and select the statistics tab. You will see the size of the database at the bottom of the list.

Then if you click on another database, it stays on the statistics tab so you can easily see many database sizes without much effort. If you open the table list, it shows all tables and their sizes.

share|improve this answer
    
And if you click the Databases tree node (attached to a DB connection) and select the Statistics tab you will be presented with a nice summary of all the Databases and their sizes (third column). – zloster Feb 15 at 14:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.