Postgres DB Size Command

后端 未结 10 553
孤独总比滥情好
孤独总比滥情好 2021-01-29 17:09

What is the command to find the size of all the databases?

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

         


        
相关标签:
10条回答
  • 2021-01-29 17:51

    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.

    0 讨论(0)
  • 2021-01-29 17:59
    -- Database Size
    SELECT pg_size_pretty(pg_database_size('Database Name'));
    -- Table Size
    SELECT pg_size_pretty(pg_relation_size('table_name'));
    
    0 讨论(0)
  • 2021-01-29 18:00
    SELECT pg_size_pretty(pg_database_size('name of database'));
    

    Will give you the total size of a particular database however I don't think you can do all databases within a server.

    However you could do this...

    DO
    $$
    DECLARE
    r   RECORD;
    db_size TEXT;
    BEGIN
    FOR r in
    SELECT datname FROM pg_database
    WHERE datistemplate = false
    LOOP
    db_size:= (SELECT pg_size_pretty(pg_database_size(r.datname)));
    
    RAISE NOTICE 'Database:% , Size:%', r.datname , db_size;
    
    END LOOP;
    END;
    $$
    
    0 讨论(0)
  • 2021-01-29 18:05

    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.

    0 讨论(0)
提交回复
热议问题