PostgreSQL服务管理

181 阅读3分钟

1、服务的启停

1.1、服务启动

使用initdb初始化一个数据库集簇后(一般为data),要使用该数据库集簇,需要先启动数据库实例,一般通过两种方式可以启动数据库实例:

  • 通过运行postgres进程启动
  • 使用pg_ctl命令启动

首先介绍通过postgres启动,启动方式如下:

[postgres@VM-0-11-centos bin]$ ./postgres -D ./data
2022-02-09 21:26:11.830 CST [16675] LOG:  starting PostgreSQL 12.2 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39), 64-bit
2022-02-09 21:26:11.830 CST [16675] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2022-02-09 21:26:11.836 CST [16675] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2022-02-09 21:26:12.003 CST [16675] LOG:  redirecting log output to logging collector process
2022-02-09 21:26:12.003 CST [16675] HINT:  Future log output will appear in directory "log".

启动后,新开一个session,查看进程

[root@VM-0-11-centos ~]# ps -ef |grep postgres
postgres 16675 14191  0 21:26 pts/1    00:00:00 ./postgres -D ./data
postgres 16682 16675  0 21:26 ?        00:00:00 postgres: logger
postgres 16684 16675  0 21:26 ?        00:00:00 postgres: checkpointer
postgres 16685 16675  0 21:26 ?        00:00:00 postgres: background writer
postgres 16686 16675  0 21:26 ?        00:00:00 postgres: walwriter
postgres 16687 16675  0 21:26 ?        00:00:00 postgres: autovacuum launcher
postgres 16688 16675  0 21:26 ?        00:00:00 postgres: archiver
postgres 16689 16675  0 21:26 ?        00:00:00 postgres: stats collector
postgres 16690 16675  0 21:26 ?        00:00:00 postgres: logical replication launcher
root     17083 17002  0 21:26 pts/0    00:00:00 grep --color=auto postgres

也可以加一个&符号,表示后台执行

[postgres@VM-0-11-centos bin]$ ./postgres -D ./data &
[1] 17838
[postgres@VM-0-11-centos bin]$ 2022-02-09 21:28:23.178 CST [17838] LOG:  starting PostgreSQL 12.2 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39), 64-bit
2022-02-09 21:28:23.178 CST [17838] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2022-02-09 21:28:23.187 CST [17838] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2022-02-09 21:28:23.204 CST [17838] LOG:  redirecting log output to logging collector process
2022-02-09 21:28:23.204 CST [17838] HINT:  Future log output will appear in directory "log".

另一种方式通过pg_ctl启动,示例如下:

[postgres@VM-0-11-centos bin]$ ./pg_ctl -D ./data start
waiting for server to start....2022-02-09 21:29:32.201 CST [18418] LOG:  starting PostgreSQL 12.2 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39), 64-bit
2022-02-09 21:29:32.201 CST [18418] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2022-02-09 21:29:32.211 CST [18418] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2022-02-09 21:29:32.228 CST [18418] LOG:  redirecting log output to logging collector process
2022-02-09 21:29:32.228 CST [18418] HINT:  Future log output will appear in directory "log".
 done
server started

启动后查看进程,可以看到实际还是调用的postgres

[postgres@VM-0-11-centos bin]$ ps -ef |grep postgres
postgres 18418     1  0 21:29 ?        00:00:00 /opt/pgsql/bin/postgres -D ./data
postgres 18419 18418  0 21:29 ?        00:00:00 postgres: logger
postgres 18421 18418  0 21:29 ?        00:00:00 postgres: checkpointer
postgres 18422 18418  0 21:29 ?        00:00:00 postgres: background writer
postgres 18423 18418  0 21:29 ?        00:00:00 postgres: walwriter
postgres 18424 18418  0 21:29 ?        00:00:00 postgres: autovacuum launcher
postgres 18425 18418  0 21:29 ?        00:00:00 postgres: archiver
postgres 18426 18418  0 21:29 ?        00:00:00 postgres: stats collector
postgres 18427 18418  0 21:29 ?        00:00:00 postgres: logical replication launcher
postgres 18614 14191  0 21:29 pts/1    00:00:00 ps -ef
postgres 18615 14191  0 21:29 pts/1    00:00:00 grep --color=auto postgres

1.2、服务停止

停止数据库服务的方法有两种:

  • 给postgres主进程发送signal信号,停止数据库,例如SIGINT、SIGKILL等
  • 使用pg_ctl命令停止数据库

使用pg_ctl命令停止数据库时,可以选择的模式有三种:smart、fast、immediate。执行pg_ctl时加-m smart或-m fast或-m immediate选择模式

  • smart:在接受此停止请求后,服务器将不运行新连接,等已有的连接全部结束后才关闭数据库。如果服务器处于联机备份模式,它将等待联机备份模式不在活动时关闭,并且在这个过程中,仍然允许超级用户建立新的连接。如果为standby数据库,会等待回复和流复制中正常会话全部终止后才停止。该停库模式用得较少,因为在这种模式下需要用户主动断开连接数据库才会停止。
  • fast:快速停止,不在允许新的连接,向所有活跃的服务器发送信号,让他们立刻退出,然后等待所有子进程退出并关闭数据库。是pg默认的停库模式。
  • immediate:立即停库,主进程postgres向所有子进程发送信号,并且立刻退出,所有的子进程也会立即退出。该模式退出会导致下次启动数据库时重放wal日志进行恢复。

2、pg_ctl工具

pg_ctl具有如下功能:

  • 初始化PostgreSQL数据库实例
  • 启动,停止、重启数据库服务
  • 重新读取配置文件
  • 查看数据库服务状态
  • 提升后备服务器为主机
  • 轮换服务器日志
  • 向一个指定进程发送信号
  • 在windows平台运行为数据库实例注册或取消一个系统服务
[postgres@VM-0-11-centos bin]$ ./pg_ctl -?
pg_ctl is a utility to initialize, start, stop, or control a PostgreSQL server.

Usage:
  pg_ctl init[db]   [-D DATADIR] [-s] [-o OPTIONS]
  pg_ctl start      [-D DATADIR] [-l FILENAME] [-W] [-t SECS] [-s]
                    [-o OPTIONS] [-p PATH] [-c]
  pg_ctl stop       [-D DATADIR] [-m SHUTDOWN-MODE] [-W] [-t SECS] [-s]
  pg_ctl restart    [-D DATADIR] [-m SHUTDOWN-MODE] [-W] [-t SECS] [-s]
                    [-o OPTIONS] [-c]
  pg_ctl reload     [-D DATADIR] [-s]
  pg_ctl status     [-D DATADIR]
  pg_ctl promote    [-D DATADIR] [-W] [-t SECS] [-s]
  pg_ctl logrotate  [-D DATADIR] [-s]
  pg_ctl kill       SIGNALNAME PID

Common options:
  -D, --pgdata=DATADIR   location of the database storage area
  -s, --silent           only print errors, no informational messages
  -t, --timeout=SECS     seconds to wait when using -w option
  -V, --version          output version information, then exit
  -w, --wait             wait until operation completes (default)
  -W, --no-wait          do not wait until operation completes
  -?, --help             show this help, then exit
If the -D option is omitted, the environment variable PGDATA is used.

Options for start or restart:
  -c, --core-files       allow postgres to produce core files(提高服务器的软限制ulimit -c,允许数据库实例发生异常时产生coredump文件)
  -l, --log=FILENAME     write (or append) server log to FILENAME
  -o, --options=OPTIONS  command line options to pass to postgres
                         (PostgreSQL server executable) or initdb
  -p PATH-TO-POSTGRES    normally not necessary

Options for stop or restart:
  -m, --mode=MODE        MODE can be "smart", "fast", or "immediate"

Shutdown modes are:
  smart       quit after all clients have disconnected
  fast        quit directly, with proper shutdown (default)
  immediate   quit without complete shutdown; will lead to recovery on restart

Allowed signal names for kill:
  ABRT HUP INT KILL QUIT TERM USR1 USR2

Report bugs to <pgsql-bugs@lists.postgresql.org>.

3、postgres及单用户模式

在启动postgres时,添加"--single"参数,这时postgres进程不会进行后台服务模式,而是进入交互式的模式,示例如下:

[postgres@VM-0-11-centos bin]$ ./postgres --single -D ./data

PostgreSQL stand-alone backend 12.2
backend>

单用户模式主要用于修复数据库的集中场景

  • 当多用户模式不接收所有命令时,可以使用单用户模式连接到数据库
  • initdb阶段
  • 修复系统表

4、服务配置

pg的配置参数是在data目录下的postgresql.conf中配置。除此之外,在数据库中,可以通过系统视图"pg_settings"查询到。例如:

查询配置参数"client_min_messages"取值

postgres=# select enumvals from pg_settings where name = 'client_min_messages';
                           enumvals
---------------------------------------------------------------
 {debug5,debug4,debug3,debug2,debug1,log,notice,warning,error}
(1 row)

查询参数"autovacuum_vacuum_cost_delay"描述

postgres=# select short_desc,extra_desc from pg_settings where name = 'autovacuum_vacuum_cost_delay';
                     short_desc                     | extra_desc
----------------------------------------------------+------------
 Vacuum cost delay in milliseconds, for autovacuum. |
(1 row)

参数的分类,pg中有些参数改变后需要重启服务器生效,有些reload生效,有些只有超级用户可修改,分为如下几类:

  • internal:只读参数,初始化时已经写死,不能改变。
  • postmaster:改变着类参数需要重启postgresql实例。
  • sighup:执行reload生效。
  • backend:reload生效,新的配置值出现在修改后的新连接。
  • superuser:可以由超级用户使用SET命令来改变,只影响自身session配置,reload后影响后续连接。
  • user:和superuser类似,只是可以使用普通用户。

查询参数的类型:

postgres=# select name,context from pg_settings where name = 'wal_buffers';
    name     |  context
-------------+------------
 wal_buffers | postmaster
(1 row)