21

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

3 Answers3

27

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
NoelProf
  • 698
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.

Emeric
  • 863
  • But where would I place the postgresql92 script? – TheLovelySausage Aug 06 '15 at 04: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 06 '15 at 05: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 06 '15 at 07: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 06 '15 at 07:46
  • did you install postgres using dnf or yum? – TheLovelySausage Aug 06 '15 at 07:55
  • I don't use a rpm-based distribution. However, if you install it via either dnf or yum, you will end up retrieving the server RPM which contains /usr/lib/systemd/system/postgresql-9.X.service. – Emeric Aug 06 '15 at 08:11
  • hi Emeric, i understand your postgresql installation places .service files in the path you mentioned... you can use systemctl enable postgresql-9.x and systemctl start postgresql-9.X... is n't it working in your systemd setup? –  Aug 06 '15 at 10:50
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"