查看所有数据库
playboy=> \l //\加上字母l,相当于mysql的,mysql> show databases;
List of databases
Name | Owner | Encoding
-----------+----------+----------
playboy | postgres | UTF8
postgres | postgres | UTF8
template0 | postgres | UTF8
template1 | postgres | UTF8
查看playboy数据库的大小
playboy=> select pg_database_size('playboy');
pg_database_size
------------------
3637896
(1 row)
以KB,MB,GB的方式来查看数据库大小
playboy=> select pg_size_pretty(pg_database_size('playboy'));
pg_size_pretty
----------------
3553 kB
(1 row)
查看所有数据库的大小
playboy=> select pg_database.datname, pg_database_size(pg_database.datname) AS size from pg_database;
dataname | size
-----------+---------
postgres | 3621512
playboy | 3637896
template1 | 3563524
template0 | 3563524
(4 rows)
统计各数据库占用磁盘大小:
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
ROM 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
查看单表
playboy=> \d test; //相当于mysql的,mysql> desc test;
Table "public.test"
Column | Type | Modifiers
--------+-----------------------+-----------
id | integer | not null
name | character varying(32) |
Indexes: "playboy_id_pk" PRIMARY KEY, btree (id)
查看表大小
playboy=> select pg_relation_size('test');
pg_relation_size
------------------
0
(1 row)
以KB,MB,GB的方式来查看表大小
playboy=> select pg_size_pretty(pg_relation_size('test'));
pg_size_pretty
----------------
0 bytes
(1 row)
统计数据库中各表占用磁盘大小:
SELECT
table_schema || '.' || table_name AS table_full_name,
pg_size_pretty(pg_total_relation_size('"' || table_schema || '"."' || table_name || '"')) AS size
FROM information_schema.tables
ORDER BY
pg_total_relation_size('"' || table_schema || '"."' || table_name || '"') DESC
查看索引
playboy=> \di //相当于mysql的,mysql> show index from test;
List of relations
Schema | Name | Type | Owner | Table
--------+---------------+-------+---------+-------
public | playboy_id_pk | index | playboy | test
(1 row)
查看表的总大小,包括索引大小
playboy=> select pg_size_pretty(pg_total_relation_size('test'));
pg_size_pretty
----------------
8192 bytes
(1 row)