PostgreSql9 查看数据库相关信息

时间:2021-07-11 03:27:59

1、检查数据库版本:

postgres=# select version();
version

--------------------------------------------------------------------------------
-----------------------------
PostgreSQL 9.4.2 on x86_64-redhat-linux-gnu, compiled by gcc (GCC) 5.1.1 201504
22 (Red Hat 5.1.1-1), 64-bit
(1 行记录)

2、查看数据库启动时间:

postgres=# select pg_postmaster_start_time();
pg_postmaster_start_time
-------------------------------
2015-06-12 19:06:23.031591+08

截至当前时间,运行了多长时间:

postgres=# select current_timestamp - pg_postmaster_start_time() as uptime;
uptime
-----------------
02:34:13.631323

postgres=# select date_trunc('second',current_timestamp-pg_postmaster_start_time()) as uptime;
uptime
----------
02:36:10

3、列出当前所有数据库:

bash-4.3$ psql -l
资料库列表
名称 | 拥有者 | 字元编码 | 校对规则 | Ctype | 存取权限
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 |
template0 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 行记录)


ostgres=# select datname from pg_database;
datname
-----------
template1
template0
postgres
(3 行记录)

4、查看数据库文件大小


postgres=# select pg_database_size(current_database());
pg_database_size
------------------
6999828

上面是当前数据库

postgres=# select current_database();
current_database
------------------
postgres

或者所有数据库文件大小:


postgres=# select sum(pg_database_size(datname)) from pg_database;
sum
----------
20738844
(1 行记录)