14

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

20

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
6
# 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.

  • But where would I place the postgresql92 script? – TheLovelySausage Aug 6 '15 at 4:57
  • You 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 Aug 6 '15 at 5:12
  • The postgresql was installed from source though, not using dnf because I need to install 3 versions of postgres in specific directories, is it possible to use the supplied start-scripts linux file to start postgresql? – TheLovelySausage Aug 6 '15 at 7:23
  • My distribution adds this script as /usr/lib/systemd/system/postgresql.service. The start-scripts provided by postgresql seem to only cover SysV. – Emeric Aug 6 '15 at 7:46
  • did you install postgres using dnf or yum? – TheLovelySausage Aug 6 '15 at 7:55
0

Posted systemctl unit file above help me a lot but to create the one you need you just have to put it on :

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

Think about changing binay pg_ctl path according to your install, and if you want to run another instance, you must also change default listening port :

ExecStart=/usr/local/pgsql/bin/pg_ctl -o "-p 5489"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.