Systemd Postgresql启动脚本


14

我正在将postgresql安装到第二台服务器上

以前我安装了postgresql,然后使用提供的脚本

./contrib/start-scripts/linux

放入正确的目录

# cp ./contrib/start-scripts/linux /etc/rc.d/init.d/postgresql92
# chmod 755 /etc/rc.d/init.d/postgresql92

然后我可以按预期执行

# service postgresql92 start

但是,新机器正在使用Systemd,看来有一种完全不同的方法来执行此操作

我不想为此而and毁,所以我想知道是否有人可以指出正确的方向,以实现相同的结果

Answers:


21

从源代码安装时,您将需要添加一个与源代码安装一起使用的systemd单元文件。对于RHEL,Fedora我的单位文件如下所示:

/usr/lib/systemd/system/postgresql.service

[Unit]
Description=PostgreSQL database server
After=network.target

[Service]
Type=forking

User=postgres
Group=postgres

# Where to send early-startup messages from the server (before the logging
# options of postgresql.conf take effect)
# This is normally controlled by the global default set by systemd
# StandardOutput=syslog

# Disable OOM kill on the postmaster
OOMScoreAdjust=-1000
# ... but allow it still to be effective for child processes
# (note that these settings are ignored by Postgres releases before 9.5)
Environment=PG_OOM_ADJUST_FILE=/proc/self/oom_score_adj
Environment=PG_OOM_ADJUST_VALUE=0

# Maximum number of seconds pg_ctl will wait for postgres to start.  Note that
# PGSTARTTIMEOUT should be less than TimeoutSec value.
Environment=PGSTARTTIMEOUT=270

Environment=PGDATA=/usr/local/pgsql/data


ExecStart=/usr/local/pgsql/bin/pg_ctl start -D ${PGDATA} -s -w -t ${PGSTARTTIMEOUT}
ExecStop=/usr/local/pgsql/bin/pg_ctl stop -D ${PGDATA} -s -m fast
ExecReload=/usr/local/pgsql/bin/pg_ctl reload -D ${PGDATA} -s

# Give a reasonable amount of time for the server to start up/shut down.
# Ideally, the timeout for starting PostgreSQL server should be handled more
# nicely by pg_ctl in ExecStart, so keep its timeout smaller than this value.
TimeoutSec=300

[Install]
WantedBy=multi-user.target

然后在启动时启用服务并启动PostgreSQL服务:

$ sudo systemctl daemon-reload # load the updated service file from disk
$ sudo systemctl enable postgresql
$ sudo systemctl start postgresql

6
# systemctl start postgresql.service

某些环境会转换service <name> startsystemctl start <name>.service,但您不必依赖它。


但是我将postgresql92脚本放在哪里?
TheLovelySausage,2015年

您不再在systemd中使用它。您的发行版应该为您提供postgresql systemd服务文件,以便您可以启动该服务。
Emeric 2015年

尽管postgresql是从源代码安装的,但未使用dnf,因为我需要在特定目录中安装3个版本的postgres,是否可以使用提供的start-scripts linux文件启动postgresql?
TheLovelySausage

我的发行版将此脚本添加为/usr/lib/systemd/system/postgresql.service。postgresql提供的起始脚本似乎仅涵盖SysV
Emeric 2015年

您是否使用dnf或yum安装了postgres?
TheLovelySausage'8

0

上面发布的systemctl单位文件对我有很大帮助,但是要创建您需要的文件,只需将其放在下面即可:

/etc/systemd/system/postgresql92.service
systemctl enable postgresql92.service
systemctl start postgresql92.service

考虑根据您的安装更改binay pg_ctl路径,如果要运行另一个实例,还必须更改默认监听端口:

ExecStart=/usr/local/pgsql/bin/pg_ctl -o "-p 5489"
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.