Systemd postgresql start script
Solution 1
When installing from source, you will need to add a systemd unit file that works with the source install. For RHEL, Fedora my unit file looks like:
/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
Then enable the service on startup and start the PostgreSQL service:
$ sudo systemctl daemon-reload # load the updated service file from disk
$ sudo systemctl enable postgresql
$ sudo systemctl start postgresql
Solution 2
# systemctl start postgresql.service
Some environments would translate service <name> start
to systemctl start <name>.service
, but you don't have to rely on it.
Related videos on Youtube
waldemar_enns
Primarily Informix-4GL Programmer, Linux Bash Programmer and Linux Administrator. Dabbler in Java, Python, HTML, PHP and JavaScript. Expert at breaking everything.
Updated on September 18, 2022Comments
-
waldemar_enns over 1 year
I'm in the process of installing postgresql onto a second server
Previously I installed postgresql and then used the supplied script
./contrib/start-scripts/linux
Placed into the correct dir
# cp ./contrib/start-scripts/linux /etc/rc.d/init.d/postgresql92 # chmod 755 /etc/rc.d/init.d/postgresql92
Which I could then execute as expected with
# service postgresql92 start
However the new machine is using Systemd and it looks like there is a completely different way to do this
I don't want to hack at this and ruin something so I was wondering if anyone out there could point me in the right direction of how to achieve the same result
-
waldemar_enns over 8 yearsBut where would I place the postgresql92 script?
-
Emeric over 8 yearsYou don't use it anymore in systemd. Your distribution should provide you with the postgresql systemd service file so that youcan start the service.
-
Emeric over 8 yearsMy distribution adds this script as
/usr/lib/systemd/system/postgresql.service
. The start-scripts provided by postgresql seem to only coverSysV
. -
waldemar_enns over 8 yearsdid you install postgres using dnf or yum?
-
Emeric over 8 yearsI don't use a
rpm
-based distribution. However, if you install it via eitherdnf
oryum
, you will end up retrieving the server RPM which contains/usr/lib/systemd/system/postgresql-9.X.service
. -
Admin over 8 yearshi Emeric, i understand your postgresql installation places
.service
files in the path you mentioned... you can usesystemctl enable postgresql-9.x
andsystemctl start postgresql-9.X
... is n't it working in your systemd setup?