Systemd postgresql start script

56,863

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.

Share:
56,863

Related videos on Youtube

waldemar_enns
Author by

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, 2022

Comments

  • waldemar_enns
    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
    waldemar_enns over 8 years
    But where would I place the postgresql92 script?
  • Emeric
    Emeric over 8 years
    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
    Emeric over 8 years
    My distribution adds this script as /usr/lib/systemd/system/postgresql.service. The start-scripts provided by postgresql seem to only cover SysV.
  • waldemar_enns
    waldemar_enns over 8 years
    did you install postgres using dnf or yum?
  • Emeric
    Emeric over 8 years
    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.
  • Admin
    Admin over 8 years
    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?