I have PostgreSQL-11 installed on Ubuntu 18.04 via apt-get, and based from here, I managed to install 2 instances of postgreSQL on port 5432 and 5433.

My Question is, can I have separate start-stop script for these two instances? As my other server running postgresql 9.6 on Centos 7, has several startup script which are:

postgresql-9.6-instanceA.service postgresql-9.6-instanceB.service postgresql-9.6-instanceC.service postgresql-9.6-instanceD.service postgresql-9.6-instanceE.service

Where those files difference only in the environment (notice the Environment section):

[root@root]# cat /etc/systemd/system/postgresql-9.6-instanceA.service
# It's not recommended to modify this file in-place, because it will be
# overwritten during package upgrades.  If you want to customize, the
# best way is to create a file "/etc/systemd/system/postgresql-9.6.service",
# containing
#       .include /lib/systemd/system/postgresql-9.6.service
#       ...make your changes here...
# For more info about custom unit files, see
# http://fedoraproject.org/wiki/Systemd#How_do_I_customize_a_unit_file.2F_add_a_custom_unit_file.3F

# Note: changing PGDATA will typically require adjusting SELinux
# configuration as well.

# Note: do not use a PGDATA pathname containing spaces, or you will
# break postgresql-setup.
Description=PostgreSQL 9.6 database server



# Note: avoid inserting whitespace in these Environment= lines, or you may
# break postgresql-setup.

# Location of database directory

# 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

ExecStartPre=/usr/pgsql-9.6/bin/postgresql96-check-db-dir ${PGDATA}
ExecStart=/usr/pgsql-9.6/bin/postmaster -D ${PGDATA}
ExecReload=/bin/kill -HUP $MAINPID

# Do not set any timeout value, so that systemd will not kill postmaster
# during crash recovery.


Based from what I read here, the script above can be created if the installation is from source, thus we need to add systemd unit file.

These are the content of the systemd script on my ubuntu server. These below are the postgresql.service file on my ubuntu 18.04: The first one is /var/lib/systemd/deb-systemd-helper-enabled/multi-user.target.wants/postgresql.service, which is empty:

root@hostname:~# ls -lah /var/lib/systemd/deb-systemd-helper-enabled/multi-user.target.wants/postgresql.service
-rw-r--r-- 1 root root 0 Feb 25 03:50 /var/lib/systemd/deb-systemd-helper-enabled/multi-user.target.wants/postgresql.service

The second and third file has the same content:

/etc/systemd/system/multi-user.target.wants/postgresql.service /lib/systemd/system/postgresql.service

# systemd service for managing all PostgreSQL clusters on the system. This
# service is actually a systemd target, but we are using a service since
# targets cannot be reloaded.

Description=PostgreSQL RDBMS



So again, can I have such situation with my ubuntu server? So that I can have start-stop-reload-restart script for my two postgres instances, as I'm not sure where or which file to edit.


Debian's packaging has already been doing this for you since 2014. Based upon your configuration files in /etc/postgresql/, a systemd unit generator is automatically creating and enabling postgresql@instance.service units.

This is documented on your system in a read-me in /usr/share/doc/postgresql-common/. Read that and all of the other doco in that directory.

Further reading

| improve this answer | |
  • yes, I just found it just a few minutes ago. Thank you for answer. Really appreaciate it. – buzzing_bee Feb 25 '19 at 8:32

I found the answer from here at askubuntu.com

Turns out that I need to specify the .service script manually after postgres installation and after creating a new instances (in this case postgres cluster) using command:

systemctl enable postgresql@<postgresversion>-<clustername>.service

so it will be:

systemctl enable postgresql@11-instanceA

| improve this answer | |

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.