---查看所有帮助
postgres=# \?
---查看SQL帮助
postgres=# \h
---切换数据库
postgres=# \c database_name
---切换用户
postgres=# \c - username
---同时切换数据库和用户
postgres=#\c database_name username
---查看连接信息
postgres=# \conninfo
---输出信息内外都显示边框
postgres=#\pset border 2
---查看 表信息
postgres=# \d
---查看索引信息
postgres=# \di
---查看视图信息
postgres=# \dv
---查看序列信息
postgres=# \ds
---查看数据库信息
postgres=# \l
---查看历史命令
postgres=# \s
---查看表空间信息
postgres=# \db
---查看schema信息
postgres=# \dn
---查看函数信息
postgres=# \df
---查看权限信息
postgres=# \dp
或者
postgres=# \z
---查看用户或者角色
postgres=# \du
或者
postgres=# \dg
---查看已安装的扩展插件
postgres=# \dx
---查看外部表
postgres=# \det
---查看user mapping
postgres=# \deu
---查看分区
postgres=# \dP
---查看分区索引
postgres=# \dPi
---查看当前数据库下当前用户下的所有触发器
postgres=# select * from pg_trigger;
---计算对象大小
postgres=# select pg_size_pretty(pg_relation_size('xx'));
---查询显示切换为竖行显示
postgres=# \x
---查看进程端口
netstat -anltp|grep postgres
---查看postgres进程
ps -ef|grep ^postgres
---查看postgres进程 ptree格式
ps -ajxf|grep postgres
---查看当前库自动commit状态,默认是on,打开自动提交
postgres=# \echo :AUTOCOMMIT
关闭当前库自动commit
postgres=#\set AUTOCOMMIT off
打开当前库自动commit
postgres=# \set AUTOCOMMIT on
---查看表的表空间号及数据文件号
postgres==# select reltablespace,relfilenode from pg_class where relname='jobs'; reltablespace | relfilenode ---------------+------------- 0 | 16408 (1 row)
0表示数据库默认表空间,16408表示该表的数据文件号
---查找配置文件位置
postgres=# show config_file;
---重新加载配置文件
postgres=# select pg_reload_conf();
---判断参数修改后是否要重启服务
postgres=# select name,setting,pending_restart from pg_settings where name='autovacuum';
name | setting | pending_restart
------------+---------+-----------------
autovacuum | on | f
(1 row)
f表示不需要重启。
---统计sql运行时间:
\timing on --打开
\timing off --关闭
或者直接执行
\timing 打开或关闭
---重复执行sql语句
test1=> select count(*) from company;\watch 2 ---重复执行sql ,每2秒执行一次,可以用ctrl+c中止
---在已运行的psql中显示了某个命令实际执行的SQL语句
\set ECHO_HIDDEN on ---打开显示
\set ECHO_HIDDEN off ---关闭显示
---查看当前事务开始时的时间戳(三种方法结果相同)
- mydb=# select now();
- mydb=# select current_timestamp;
- mydb=# select transaction_timestamp();
transaction_timestamp
-------------------------------
2022-08-02 17:20:39.969107+08
-----查看已安装的扩展函数---
postgres=# \dx
List of installed extensions
Name | Version | Schema | Description
---------------+---------+------------+----------------------------------------------------------------
pg_visibility | 1.2 | public | examine the visibility map (VM) and page-level visibility info
plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language
postgres_fdw | 1.1 | public | foreign-data wrapper for remote PostgreSQL servers
(3 rows)
----查看函数提供的具体功能模块
postgres=# \dx+ postgres_fdw;
Objects in extension "postgres_fdw"
Object description
---------------------------------------------
foreign-data wrapper postgres_fdw
function postgres_fdw_disconnect_all()
function postgres_fdw_disconnect(text)
function postgres_fdw_get_connections()
function postgres_fdw_handler()
function postgres_fdw_validator(text[],oid)
(6 rows)
postgres=# \dx+ pg_visibility
Objects in extension "pg_visibility"
Object description
-----------------------------------------------
function pg_check_frozen(regclass)
function pg_check_visible(regclass)
function pg_truncate_visibility_map(regclass)
function pg_visibility_map(regclass)
function pg_visibility_map(regclass,bigint)
function pg_visibility_map_summary(regclass)
function pg_visibility(regclass)
function pg_visibility(regclass,bigint)
(8 rows)
----授予普通用户使用扩展包函数权限
grant execute on function pg_visibility_map to XX;
----查看归档情况
select * from pg_stat_archiver;
----查看数据库及默认表空间
$oid2name
All databases: Oid Database Name Tablespace ---------------------------------- 17138 gjjdb pg_default 17174 jianyedb tbs1 17707 mydb pg_default 17139 nongkendb pg_default 13892 postgres pg_default 13891 template0 pg_default 1 template1 pg_default 17294 test1 pg_default
-------查看cluster中所有namespace(schema),包括系统schema
postgres=# select * from pg_catalog.pg_namespace ;
其中information_schema是方便用户查看表/视图/函数信息提供的,它大多是视图,MySQL,SQL Server同样有information_schema这个schema。
pg_catalog是系统Schema,包含了系统的自带函数/数据类型定义等,pg_catalog是保障postgres正常运转的重要基石。
--------查看cluster所有的schema
postgres=# \dn
---查看在指定表空间中拥有对象的数据库集合
test1=# select * from pg_tablespace; test1=# select pg_tablespace_databases(1663); test1=# select oid,datname from pg_database;
-----切换新的系统日志
test1=# select pg_rotate_logfile();
------查看某个数据库大小
select pg_database_size('数据库名');
------查看多个数据库大小
select pg_database.datname, pg_size_pretty(pg_database_size(pg_database.datname)) AS size from pg_database;
-------按顺序查看public schema下的表索引大小
select indexrelname, pg_size_pretty(pg_relation_size(relid))
from pg_stat_user_indexes where schemaname='public'
order by pg_relation_size(relid) desc;
-------查看public schema所有表的大小
select relname, pg_size_pretty(pg_relation_size(relid))
from pg_stat_user_tables
where schemaname='public'
order by pg_relation_size(relid) desc;
------查看public schema下指定表的大小
select relname, pg_size_pretty(pg_relation_size(relid))
from pg_stat_user_tables
where schemaname='public' and relname='表名'
order by pg_relation_size(relid) desc;
----查看当前日志文件名
postgres=# select pg_walfile_name(pg_current_wal_lsn());
pg_walfile_name
--------------------------
00000001000000000000002B
(1 row)
----查看当前日志文件名及日志偏移量
postgres=# SELECT * FROM pg_walfile_name_offset(pg_current_wal_lsn());
file_name | file_offset
--------------------------+-------------
00000001000000000000002B | 1672
(1 row)