查看角色及用户
select * from sys_user;
TEST=#\du
select rolname from sys_roles;
查看会话进程/数据库连接情况
select * from sys_stat_activity;
select * from pg_stat_activity;
查询数据库总连接数
select count(*) from pg_stat_activity;
select count(*) from sys_stat_activity;
查看当前各个账号的会话数
select count(*), usename from sys_stat_activity group by usename;
查询最大连接数
show max_connections;
查询超级连接数(超级用户保留连接数)
show superuser_reserved_connections;
查看系统所有表
SELECT * FROM sys_tables;
查看所有数据库
TEST=#\l
select datname from sys_database;
select * from sys_database;
查看登录用户下的所有表:
select table_name from user_tables;
查看表空间
select * from sys_tablespace;
查看表结构
\d 表名
查数据库字符集/编码
TEST=# \l
TEST=# select datname,sys_encoding_to_char(ENCODING) from sys_database;
查看服务端编码
show server_encoding;
查看客户端编码
show client_encoding;
查询单个表的大小
select sys_size_pretty(sys_relation_size('表名'));
当前库下所有表的大小
select schemaname,relname,sys_size_pretty(sys_total_relation_size(relid)) from sys_stat_user_tables order by sys_relation_size(relid) desc;
查看单个数据库DBname所占的磁盘空间大小
select sys_database_size('DBname') ; --单位Bytes
select sys_database_size('DBname')/1024/1024/1024; --单位GB
推荐使用(使用最合适的单位输出结果)
select sys_size_pretty(sys_database_size('DBname'));
查所有库大小
select sys_database.datname, sys_database_size(sys_database.datname) as size from sys_database order by size desc; --单位Bytes
select sys_database.datname, sys_database_size(sys_database.datname)/1024/1024 as size from sys_database order by size desc; --单位MB
select sys_database.datname, sys_database_size(sys_database.datname)/1024/1024 as "size_MB" from sys_database order by "size_MB" desc; --单位MB
select sys_database.datname, sys_database_size(sys_database.datname)/1024/1024/1024 as size from sys_database order by size desc; --单位GB
select sys_database.datname, sys_database_size(sys_database.datname)/1024/1024/1024 as "size_GB" from sys_database order by "size_GB" desc; --单位GB
推荐使用(使用最合适的单位输出结果)
select sys_database.datname, sys_size_pretty(sys_database_size(sys_database.datname)) as size from sys_database order by size desc;
统计库/表的记录数
统计某个schema中各个表的记录数
SELECT schemaname,relname,n_live_tup FROM sys_stat_user_tables where schemaname='XX' ORDER BY relname DESC;
统计当前库中各个表的记录数
SELECT schemaname,relname,n_live_tup FROM sys_stat_user_tables;
SELECT schemaname,relname,n_live_tup FROM sys_stat_user_tables ORDER BY n_live_tup DESC;
查询当前搜索路径
SHOW search_path;
查看用户有效期
SELECT USENAME, VALUNTIL FROM SYS_USER;
SELECT USENAME, VALUNTIL FROM SYS_USER WHERE USENAME = 'XX';
网站声明:如果转载,请联系本站管理员。否则一切后果自行承担。
添加我为好友,拉您入交流群!
请使用微信扫一扫!