Postgres DB Size Command

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

    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.

提交回复
热议问题