PostgreSQL基本管理命令
** Command **
查看数据库当前版本:
mydb=# select version();
version
-----------------------------------------------------------------------------------
PostgreSQL 9.6.9 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (
Red Hat 4.8.5-11), 64-bit
(1 row)
查看数据库启动时间(Postmaster进程启动时间,postgres命令就是postmaster命令的一个软连接):
mydb=# select pg_postmaster_start_time();
pg_postmaster_start_time
-------------------------------
2018-06-20 14:06:46.334028+08
(1 row)
查看最后load配置文件的时间:
mydb=# select pg_conf_load_time();
pg_conf_load_time
-------------------------------
2018-06-20 14:06:46.213823+08
(1 row)
(使用pg_ctl reload的命令可以重新装载数据文件, 对类型为sighup和backend的参数可以重新装载,区别在于backend只对后续连接有效)
查看有哪些数据库, 除了使用psql命令进入sql界面后执行\l之外,还可以用psql -l直接查看;
[postgres@orastb ~]$ psql -l -p 5433
List of databases
Name | Owner | Encoding | Collate | Ctype | Access pri
vileges
----------------+----------+----------+-------------+-------------+-------------
mydb | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
mydb_backup_01 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 |
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
(5 rows)
查看session_user和current_user:
mydb=# select session_user;
session_user
--------------
postgres
(1 row)
mydb=# select current_user;
current_user
--------------
postgres
(1 row)
mydb=# select user;
current_user
--------------
postgres
(1 row)
通常session_user和current_user是一致的,但是如果使用set role命令,current_user会改变,而session_user一直记录登录session的最初用户; (current_user和user是等价的);
查看当前参数设置:
mydb=# show shared_buffers;
shared_buffers
----------------
4GB
(1 row)
mydb=# select current_setting('shared_buffers');
current_setting
-----------------
4GB
(1 row)
修改当前session的参数:
mydb=# show maintenance_work_mem;
maintenance_work_mem
----------------------
128MB
(1 row)
mydb=# select set_config('maintenance_work_mem' , '256MB' ,false);
set_config
------------
256MB
(1 row)
这里使用set maintenance_work_mem to '256MB';是一样的.
查看当前session所在客户端的、服务器IP地址以及Port,以及查看当前Session 的PID:
mydb=# select inet_client_addr(), inet_client_port();
inet_client_addr | inet_client_port
------------------+------------------
|
(1 row)
mydb=# select inet_server_addr(), inet_server_port();
inet_server_addr | inet_server_port
------------------+------------------
|
(1 row)
mydb=# select pg_backend_pid();
pg_backend_pid
----------------
13219
(1 row)
查看当前正在写的WAL(Write Ahead Log)文件:
mydb=# select pg_xlogfile_name(pg_current_xlog_location());
pg_xlogfile_name
--------------------------
000000010000000000000001
(1 row)
mydb=# select pg_current_xlog_location();
pg_current_xlog_location
--------------------------
0/19B8638
(1 row)
查看WAL Buffer中有多少数据还没有落盘:
mydb=# select pg_xlog_location_diff(pg_current_xlog_insert_location(), pg_current_xlog_location());
pg_xlog_location_diff
-----------------------
0
(1 row)
这里看到,WAL Buffer中的信息已经全部写入Disk, 这里pg_current_xlog_insert_location()可以获取写入的位置(Oracle中On-disk RBA), 而pg_current_xlog_location()可以获取当前的xlog生成的位置(相当于Oracle中最新一条事务操作对应的RBA);
查看数据库实例是否正在做基础备份:
mydb=# select pg_is_in_backup(), pg_backup_start_time();
pg_is_in_backup | pg_backup_start_time
-----------------+----------------------
f |
(1 row)
查看数据库是否处于Hot Standby状态:
mydb=# select pg_is_in_recovery();
pg_is_in_recovery
-------------------
f
(1 row)
查看数据库大小:
mydb=# select pg_database_size('mydb') ,pg_size_pretty(pg_database_size('mydb'));
pg_database_size | pg_size_pretty
------------------+----------------
8026648 | 7839 kB
如果只想查看表及索引大小,可以使用 select pg_size_pretty(pg_total_relation_size('mydb'));
如果只想看表的,则使用pg_relation_size()即可;
** Maintenance Command **
Reload配置文件:
pg_ctl reload;
or :
mydb=# select pg_reload_conf();
pg_reload_conf
----------------
t
(1 row)
切换系统日志, 切换Write Ahead Log:
mydb=# select pg_rotate_logfile();
pg_rotate_logfile
-------------------
t
(1 row)
mydb=# select pg_switch_xlog();
pg_switch_xlog
----------------
0/19B8650
(1 row)
想要产生一次checkpoint, 则直接键入命令checkpoint即可;
** 取消一个长SQL,或者Kill会话 **
Session 2:
mydb=# select pg_backend_pid();
pg_backend_pid
----------------
28622
(1 row)
Session 1:
mydb=# select pg_cancel_backend(28622);
pg_cancel_backend
-------------------
t
(1 row)
mydb=# select pg_terminate_backend(28622);
pg_terminate_backend
----------------------
t
(1 row)
Session 2:
mydb=# \d
FATAL: terminating connection due to administrator command
server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
The connection to the server was lost. Attempting reset: Succeeded.
或者从操作进行Kill,ps -ef | grep 28622 ; kill -9 28622.