Answers:
您可以从“ pg_datbase”系统表中获取可以连接到的所有数据库的名称。只需将函数应用于名称,如下所示。
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;
如果您希望输出由机器而不是人类使用,则可以剪切pg_size_pretty()函数。
pg_database_size()
包括索引的大小
\l+
。不过,输出格式更易于阅读(列较少)。可写性与可读性之间的权衡…
-- Database Size
SELECT pg_size_pretty(pg_database_size('Database Name'));
-- Table Size
SELECT pg_size_pretty(pg_relation_size('table_name'));
基于答案在这里通过@Hendy Irawan
显示数据库大小:
\l+
例如
=> \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 |
显示表格大小:
\d+
例如
=> \d+
public | tuneeca_prd | table | tomcat | 8192 bytes |
public | tuneeca_stg | table | tomcat | 1464 kB |
仅适用于psql
。
\d+ *
工作,平淡地\d+
回来了Did not find any relations.
\c dbname
),然后执行\d+
。
是的,在Postgres中有一个命令可以查找数据库的大小。如下:
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;
SELECT database_name, pg_size_pretty(size) from (SELECT pg_database.datname as "database_name", pg_database_size(pg_database.datname) AS size FROM pg_database ORDER by size DESC) as ordered;
SELECT pg_database.datname AS "DB Name", pg_size_pretty(pg_database_size(pg_database.datname)) AS "Size" FROM pg_database ORDER BY (pg_database_size(pg_database.datname)) DESC;
。
SELECT pg_size_pretty(pg_database_size('name of database'));
这将为您提供特定数据库的总大小,但是我认为您无法在服务器内完成所有数据库。
但是你可以这样做...
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;
$$
pg_database
而不是这个丑陋的pl / pgsql?
注意:用户无法连接到的数据库将按照无限大小进行排序。
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
该页面还包含用于查找最大关系和最大表的大小的代码段。
您可以使用下面的查询来查找PostgreSQL所有数据库的大小。
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;
启动pgAdmin,连接到服务器,单击数据库名称,然后选择“统计信息”选项卡。您将在列表底部看到数据库的大小。
然后,如果您单击另一个数据库,则该数据库将保留在“统计信息”选项卡上,因此您可以轻松查看许多数据库大小,而无需花费很多精力。如果打开表列表,它将显示所有表及其大小。
Databases
树节点(附加到数据库连接)并选择Statistics
选项卡,将为您提供所有数据库及其大小的摘要(第三列)。
du -k /var/lib/postgresql/ |sort -n |tail
du -k /var/lib/pgsql/ | sort -n | tail