系统环境:
Centos 7.6
PostgreSQL 13 RC1
1. 创建postgres用户和组
# groupadd dba -g 1000 # useradd postgres -g 1000 -u 1000 # echo "PostgreSQL@(2020)"|passwd postgres --stdin
复制
建议固化uid与gid,统一配置流复制或集群。
2. 创建目录
# mkdir -p {/opt/pg13,/opt/data5413} # chown -R postgres: {/opt/pg13,/opt/data5413} # chmod 0755 /opt/pg13 # chmod 0700 /opt/data5413
复制
推荐数据目录带上端口号,尤其当存在多个实例时。
3. 编译安装
下载文件并解压
# su - postgres $ wget https://ftp.postgresql.org/pub/source/v13rc1/postgresql-13rc1.tar.gz $ tar -zxvf postgresql-13rc1.tar.gz
复制
with-systemd参数说明(9.6增加):
Add configure option --with-systemd to enable calling sd_notify() at server start and stop (Peter Eisentraut)
This allows the use of systemd service units of type notify, which greatly simplifies the management of PostgreSQL under systemd.
当我们使用systemd管理数据库服务时,Type方式如果为notify时,需要使用with-systemd参数,否则当我们使用systemctl管理数据库服务时会夯住,此时数据库其实已经启动并可接受连接。当使用Type方式为forking时不受影响。
编译
$ cd postgresql-13rc1/ $ ./configure --prefix=/opt/pg13 --with-systemd
复制
出现如下错误
configure: error: header file <systemd/sd-daemon.h> is required for systemd support
复制
安装systemd-devel包解决
# yum localinstall /media/Packages/systemd-devel-219-62.el7.x86_64.rpm
复制
安装
$ gmake world
复制
当看到最后一行显示为:
PostgreSQL, contrib, and documentation successfully made. Ready to install.
复制
说明已经编译成功
$ gmake install-world //包含扩展包和文档
复制
当看到最后一行显示为:
PostgreSQL, contrib, and documentation installation complete.
复制
说明已经安装成功
查看版本
$ /opt/pg13/bin/postgres --version postgres (PostgreSQL) 13rc1
复制
4. 初始化数据目录
$ /opt/pg13/bin/initdb -D/opt/data5413 \ -EUTF8 \ -Upostgres \ -W
复制
5. 修改数据库参数
$ vi /opt/data5413/postgresql.conf listen_addresses='0.0.0.0' port=5413 logging_collector=on log_destination=csvlog log_filename='pg_log_%u.log' log_file_mode=0600 log_truncate_on_rotation=on log_rotation_age=1d
复制
6.使用pg_ctl启停数据库
启动数据库
$ /opt/pg13/bin/pg_ctl start -D /opt/data5413
复制
停止数据库
$ /opt/pg13/bin/pg_ctl stop -D /opt/data5413
复制
7.使用systemctl管理服务
# vi /usr/lib/systemd/system/postgresql-13.service [Unit] Description=PostgreSQL 13 database server After=syslog.target network.target [Service] Type=forking TimeoutSec=120 User=postgres Environment=PGDATA=/opt/data5413 ExecStart=/opt/pg13/bin/pg_ctl start -w -D "/opt/data5413/" -l "/opt/data5413/log/startup.log" ExecStop=/opt/pg13/bin/pg_ctl stop -m fast -w -D "/opt/data5413/" ExecReload=/opt/pg13/bin/pg_ctl reload -D "/opt/data5413/" [Install] WantedBy=multi-user.target
复制
重新加载服务配置文件
systemctl daemon-reload
复制
通过systemctl启停服务
systemctl start postgresql-13 systemctl stop postgresql-13 systemctl reload postgresql-13 systemctl restart postgresql-13
复制
观察控制台日志,启停正常
8.使用pg_ctl和systemctl交叉测试启停服务
8.1 pg_ctl先启动服务
postgres用户使用pg_ctl启动服务
$ /opt/pg13/bin/pg_ctl start -D /opt/data5413
复制
再通过systemctl查看状态
systemctl status postgresql-13 ● postgresql-13.service - PostgreSQL 13 database server Loaded: loaded (/usr/lib/systemd/system/postgresql-13.service; disabled; vendor preset: disabled) Active: inactive (dead)
复制
发现服务未启动的
通过ps查看其实服务是正常启动的
$ ps f -u postgres PID TTY STAT TIME COMMAND 7844 ? S 0:00 sshd: postgres@pts/1 7845 pts/1 Ss 0:00 \_ -bash 4214 ? S 0:00 sshd: postgres@pts/0 4215 pts/0 Ss 0:00 \_ -bash 19324 pts/0 R+ 0:00 \_ ps f -u postgres 19303 ? Ss 0:00 /opt/pg13/bin/postgres -D /opt/data5413 19304 ? Ss 0:00 \_ postgres: logger 19308 ? Ss 0:00 \_ postgres: checkpointer 19309 ? Ss 0:00 \_ postgres: background writer 19310 ? Ss 0:00 \_ postgres: walwriter 19311 ? Ss 0:00 \_ postgres: autovacuum launcher 19312 ? Ss 0:00 \_ postgres: stats collector 19313 ? Ss 0:00 \_ postgres: logical replication launcher
复制
那通过systemctl是否可以stop掉pg_ctl启动的服务呢?答案是否定的
再使用systemctl启动服务试试
systemctl start postgresql-13 Job for postgresql-13.service failed because the control process exited with error code. See "systemctl status postgresql-13.service" and "journalctl -xe" for details.
复制
执行是失败的,查看下启动日志
tailf -n 3 /opt/data5413/log/startup.log 2020-12-23 11:03:29.490 CST [19154] HINT: Future log output will appear in directory "log". 2020-12-23 11:14:42.066 CST [19432] FATAL: lock file "postmaster.pid" already exists 2020-12-23 11:14:42.066 CST [19432] HINT: Is another postmaster (PID 19303) running in data directory "/opt/data5413"?
复制
很明显:通过pg_ctl启动的服务是正常的,因此systemctl直接退出。
pg_ctl先正常关闭服务
$ /opt/pg13/bin/pg_ctl stop -D /opt/data5413
复制
8.2 systemctl先启动服务
测试通过systemctl启动的服务,使用pg_ctl是否能正常关闭
# systemctl start postgresql-13
复制
查看状态
# systemctl status postgresql-13
复制
服务运行状态正常
通过pg_ctl关闭服务
$ /opt/pg13/bin/pg_ctl stop -D /opt/data5413 waiting for server to shut down.... done server stopped
复制
可以正常关闭
也可以再通过systemctl进行启动
9.小结
1.使用pg_ctl启动服务后不能通过systemctl启停服务,需要通过pg_ctl正常关闭后才可操作。
2.使用systemctl启动服务后,可以通过pg_clt来进行管理
推荐方式:尽量不要交叉使用,要么使用root用户或者有sudo权限的用户通过systemctl来管理服务,要么使用宿主用户postgres通过pg_ctl来管理。