Postgres DB Size Command

后端 未结 10 552
孤独总比滥情好
孤独总比滥情好 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:41

    You can enter the following psql meta-command to get some details about a specified database, including its size:

    \l+ <database_name>
    

    And to get sizes of all databases (that you can connect to):

    \l+
    
    0 讨论(0)
  • 2021-01-29 17:41

    Yes, there is a command to find the size of a database in Postgres. It's the following:

    SELECT pg_database.datname as "database_name", pg_size_pretty(pg_database_size(pg_database.datname)) AS size_in_mb FROM pg_database ORDER by size_in_mb DESC;
    
    0 讨论(0)
  • 2021-01-29 17:49

    You can use below query to find the size of all databases of PostgreSQL.

    Reference is taken from this blog.

    SELECT 
        datname AS DatabaseName
        ,pg_catalog.pg_get_userbyid(datdba) AS OwnerName
        ,CASE 
            WHEN pg_catalog.has_database_privilege(datname, 'CONNECT')
            THEN pg_catalog.pg_size_pretty(pg_catalog.pg_database_size(datname))
            ELSE 'No Access For You'
        END AS DatabaseSize
    FROM pg_catalog.pg_database
    ORDER BY 
        CASE 
            WHEN pg_catalog.has_database_privilege(datname, 'CONNECT')
            THEN pg_catalog.pg_database_size(datname)
            ELSE NULL
        END DESC;
    
    0 讨论(0)
  • 2021-01-29 17:49
    du -k /var/lib/postgresql/ |sort -n |tail
    
    0 讨论(0)
  • 2021-01-29 17:51

    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.

    0 讨论(0)
  • 2021-01-29 17:51

    From the PostgreSQL wiki.


    NOTE: 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.

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