When working with postgresql on Linux, most administrators use systemctl commands to start, stop, or restart database clusters. The service file that makes these operations possible is often overlooked. Understanding how this service file works gives you insight into how PostgreSQL integrates with the systemd init system. It also shows how individual database clusters are managed.
In this article, we will examine the PostgreSQL systemd service template used on Debian and Ubuntu-based systems. We will explore its structure. We will understand the purpose of each section. We will also explain the role of directives that control service startup, shutdown, dependency handling, logging, and process management. By the end, you will have an understanding of how PostgreSQL services are configured and managed at the operating system level.
How to Find and View the PostgreSQL Service File
Step 1: List Your PostgreSQL Clusters with pg_lsclusters
Check the current clusters on your system.
cybrosys@cybrosys:~$ pg_lsclusters
Result:
Ver Cluster Port Status Owner Data directory Log file
17 main 5434 online postgres /var/lib/postgresql/17/main /var/log/postgresql/postgresql-17-main.log
18 main 5432 online postgres /var/lib/postgresql/18/main /var/log/postgresql/postgresql-18-main.log
18 main2 5435 online postgres /var/lib/postgresql/18/main2 /var/log/postgresql/postgresql-18-main2.log
19 main 5433 online postgres /var/lib/postgresql/19/main /var/log/postgresql/postgresql-19-main.log
Step 2: View a Cluster’s Service File with systemctl cat
Check the contents of the service file related to PostgreSQL version 18 like this.
cybrosys@cybrosys:~$ sudo systemctl cat postgresql@18-main.service
Result:
# /lib/systemd/system/postgresql@.service
# systemd service template for PostgreSQL clusters. The actual instances will
# be called "postgresql@version-cluster", e.g. "postgresql@9.3-main". The
# variable %i expands to "version-cluster", %I expands to "version/cluster".
# (%I breaks for cluster names containing dashes.)
[Unit]
Description=PostgreSQL Cluster %i
AssertPathExists=/etc/postgresql/%I/postgresql.conf
RequiresMountsFor=/etc/postgresql/%I /var/lib/postgresql/%I
PartOf=postgresql.service
ReloadPropagatedFrom=postgresql.service
Before=postgresql.service
# stop server before networking goes down on shutdown
After=network.target
[Service]
Type=forking
# -: ignore startup failure (recovery might take arbitrarily long)
# the actual pg_ctl timeout is configured in pg_ctl.conf
ExecStart=-/usr/bin/pg_ctlcluster --skip-systemctl-redirect %i start
TimeoutStartSec=infinity
ExecStop=/usr/bin/pg_ctlcluster --skip-systemctl-redirect -m fast %i stop
TimeoutStopSec=1h
ExecReload=/usr/bin/pg_ctlcluster --skip-systemctl-redirect %i reload
PIDFile=/run/postgresql/%i.pid
SyslogIdentifier=postgresql@%i
# prevent OOM killer from choosing the postmaster (individual backends will
# reset the score to 0)
OOMScoreAdjust=-900
# restarting automatically will prevent "pg_ctlcluster ... stop" from working,
# so we disable it here. Also, the postmaster will restart by itself on most
# problems anyway, so it is questionable if one wants to enable external
# automatic restarts.
#Restart=on-failure
# (This should make pg_ctlcluster stop work, but doesn't:)
#RestartPreventExitStatus=SIGINT SIGTERM
[Install]
WantedBy=multi-user.target
What Is the PostgreSQL Service File?
Here, you can see that this service file is not specifically for a single postgres instance, but a single service file can be used by all Postgres instances.
PostgreSQL Service File Location: postgresql@.service
Here, we can see the path of this file.
# /lib/systemd/system/postgresql@.service
This is the default service template file for postgresql
We can open this file separately.
cat /lib/systemd/system/postgresql@.service
The 3 Sections of a PostgreSQL systemd Service File
In every systemd file, there are three parts
Purpose:
Here, this section mainly describes the service, when it should start, and what the things this service depends on.
Purpose:
This section mainly describes how the service should be started, how it should be managed, and how it should be stopped.
Purpose:
This defines how this service should be enabled during the time of system restart or boot.
[Unit] Section: Dependencies, Mount Points and Start Order
Now, let’s explore in more detail the Unit section in the service file of postgres.
[Unit]
Description=PostgreSQL Cluster %i
AssertPathExists=/etc/postgresql/%I/postgresql.conf
RequiresMountsFor=/etc/postgresql/%I /var/lib/postgresql/%I
PartOf=postgresql.service
ReloadPropagatedFrom=postgresql.service
Before=postgresql.service
# stop server before networking goes down on shutdown
After=network.target
Description: Naming Each PostgreSQL Cluster
Description=PostgreSQL Cluster %i
This is just a human-readable description, and the %i denotes the version of Postgres.
This means if we start postgres 18 like this
sudo systemctl start postgresql@18-main.service.
Now, the description may look like this:
Description=PostgreSQL Cluster 18
Check a Cluster’s Status with systemctl status
We can also check the status of this postgres 18 cluster like this.
cybrosys@cybrosys:/lib/systemd/system$ sudo systemctl status postgresql@18-main
Result:
? postgresql@18-main.service - PostgreSQL Cluster 18-main
Loaded: loaded (/lib/systemd/system/postgresql@.service; enabled-runtime; vendor preset: enabled)
Drop-In: /etc/systemd/system/postgresql@18-main.service.d
+-override.conf
Active: active (running) since Fri 2026-11-20 11:27:51 IST; 24h ago
Main PID: 317503 (postgres)
Tasks: 17 (limit: 36772)
Memory: 4.1G
CPU: 2min 40.504s
CGroup: /system.slice/system-postgresql.slice/postgresql@18-main.service
+- 317503 /usr/lib/postgresql/18/bin/postgres -D /var/lib/postgresql/18/main -c config_file=/etc/postgresql/18/main/postgresql.conf
+- 317521 "postgres: 18/main: io worker 0" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" >
+- 317522 "postgres: 18/main: io worker 1" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" >
+- 317523 "postgres: 18/main: io worker 2" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" >
+- 317524 "postgres: 18/main: checkpointer " "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ">
+- 317525 "postgres: 18/main: background writer " "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "">
+- 317527 "postgres: 18/main: walwriter " "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ">
+- 317528 "postgres: 18/main: autovacuum launcher " "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" >
+- 317529 "postgres: 18/main: logical replication launcher " "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" >
+- 319020 "postgres: 18/main: cybrosys postgres 127.0.0.1(43394) idle" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "">
+- 948831 "postgres: 18/main: cybrosys odoo_17 127.0.0.1(48800) idle" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" >
+-1013314 "postgres: 18/main: postgres postgres [local] idle" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "">
+-1018611 "postgres: 18/main: " "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ">
+-1019662 "postgres: 18/main: postgres postgres [local] idle" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "">
+-1028531 "postgres: 18/main: postgres postgres [local] idle" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "">
+-1188523 "postgres: 18/main: postgres odoo17_to19_combined_test 127.0.0.1(59910) idle" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" >
+-1188525 "postgres: 18/main: postgres odoo17_to19_combined_test 127.0.0.1(59922) idle" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" >
Nov 20 11:27:48 cybrosys systemd[1]: Starting PostgreSQL Cluster 18-main...
Nov 20 11:27:51 cybrosys systemd[1]: Started PostgreSQL Cluster 18-main.
AssertPathExists: Checking postgresql.conf Before Startup
AssertPathExists=/etc/postgresql/%I/postgresql.conf
This is a safety check before starting PostgreSQL. Without postgresql.conf, the PostgreSQL service will not start.
RequiresMountsFor: Making Sure Data Directories Are Mounted
RequiresMountsFor=/etc/postgresql/%I /var/lib/postgresql/%I
These are the required storage folders for the postgresql service. Suppose we are using SSD-like storage; we can change this path based on our needs.
PartOf: Linking Each Cluster to postgresql.service
PartOf=postgresql.service
This specifies that this cluster is related to the postgres.service. So whatever operations are performed on the postgres service that are also applied to these postgres clusters also.
This means if we start the postgres.service, it will individually start these postgres clusters.
Sudo systemctl start postgresql
Internally it starts
sudo systemctl start postgresql@18-main
ReloadPropagatedFrom: Reloading All Clusters at Once
ReloadPropagatedFrom=postgresql.service
This is related to the postgres service reload. If we restart the postgres service like this, internally it will restart the postgresql clusters like this.
Sudo systemctl start postgresql
Internally, it looks like this
sudo systemctl restart postgresql@18-main
Before=postgresql.service: Cluster Start Order
Before=postgresql.service
This indicates the order of starting and stopping the postgres clusters before the start and shutdown of postgres service
Now, based on this configuration, the postgres cluster will start before the postgres service.
After=network.target: Starting After the Network Is Up
After=network.target
This line has two effects.
During startup, PostgreSQL starts after the networking subsystem is initialized.
During shutdown, PostgreSQL stops before the networking subsystem is shut down.
[Service] Section: How PostgreSQL Starts, Stops and Reloads
Now, let’s look at the service section in more detail.
[Service]
Type=forking
# -: ignore startup failure (recovery might take arbitrarily long)
# the actual pg_ctl timeout is configured in pg_ctl.conf
ExecStart=-/usr/bin/pg_ctlcluster --skip-systemctl-redirect %i start
TimeoutStartSec=infinity
ExecStop=/usr/bin/pg_ctlcluster --skip-systemctl-redirect -m fast %i stop
TimeoutStopSec=1h
ExecReload=/usr/bin/pg_ctlcluster --skip-systemctl-redirect %i reload
PIDFile=/run/postgresql/%i.pid
SyslogIdentifier=postgresql@%i
# prevent OOM killer from choosing the postmaster (individual backends will
# reset the score to 0)
OOMScoreAdjust=-900
# restarting automatically will prevent "pg_ctlcluster ... stop" from working,
# so we disable it here. Also, the postmaster will restart by itself on most
# problems anyway, so it is questionable if one wants to enable external
# automatic restarts.
#Restart=on-failure
# (This should make pg_ctlcluster stop work, but doesn't:)
#RestartPreventExitStatus=SIGINT SIGTERM
Type=forking: Why PostgreSQL Uses a Forking Service
Type=forking
This defines the type of service. Actually, Postgres is started by forking a process. That’s why the service type is forking here.
ExecStart and TimeoutStartSec: Starting a Cluster with pg_ctlcluster
ExecStart=-/usr/bin/pg_ctlcluster --skip-systemctl-redirect %i start
This defines the path of the binary file named pg_ctlcluster for starting this service.
TimeoutStartSec=infinity
It also has a timeout for starting this cluster service, and here it is infinity.
ExecStop and TimeoutStopSec: Fast Shutdown Mode
ExecStop=/usr/bin/pg_ctlcluster --skip-systemctl-redirect -m fast %i stop
This defines the path of the binary file named pg_ctlcluster for stopping this service.
TimeoutStopSec=1h
The timeout for stopping the postgres service.
ExecReload: Reloading Configuration Without a Restart
ExecReload=/usr/bin/pg_ctlcluster --skip-systemctl-redirect %i reload
The path of pg_ctlcluster for reloading the postgres service.
PIDFile: Where PostgreSQL Stores Process ID Files
PIDFile=/run/postgresql/%i.pid
Defines the process ID file path
You can see the process ID files of postgres inside this path.
cybrosys@cybrosys:/var/run/postgresql$ ls
Result:
17-main.pid 18-main2.pid 18-main.pid 19-main.pid pgbouncer.pid
SyslogIdentifier: Tagging PostgreSQL Messages in System Logs
SyslogIdentifier=postgresql@%i
This is used to set the name (tag) that appears in system logs for messages generated by the service.
OOMScoreAdjust=-900: Protecting PostgreSQL from the OOM Killer
OOMScoreAdjust=-900
This tells the Linux kernel to strongly protect the PostgreSQL main server process from being selected by the Out-Of-Memory (OOM) Killer when the system runs out of memory.
The value of -900 means strong protection.
Restart=on-failure and RestartPreventExitStatus: Automatic Restart Settings
#Restart=on-failure
This means that the service will automatically restart if it exits because of a failure.
#RestartPreventExitStatus=SIGINT SIGTERMSIGINT and SIGTERM are termination signals used to ask a process to stop gracefully. So, we can also stop the automatic restart in a failure scenario when it gets the SIGINT and SIGTERM signals.
[Install] Section: Starting PostgreSQL Automatically at Boot
Now, let’s look at the install section more in detail.
[Install]
WantedBy=multi-user.target
This tells systemd to start the service automatically during boot when the system reaches the normal multi-user operating state, provided the service has been enabled.
The PostgreSQL systemd service file has a job in running database clusters in a smooth and steady way. Many admins use simple systemctl commands to talk to PostgreSQL. The service template tells the operating system exactly how to start, stop, reload, and watch each cluster.
You can also change how a cluster behaves and see what is happening inside when you manage PostgreSQL instances. This understanding is very handy for database administrators who run PostgreSQL clusters or who need more control over their server environment.