PostgresQL 11 Database Cluster Shuts Down Instantly – Fix Guide

debian-stretchpostgresqlsystemd

I have installed PostgresQL 11 via apt-get on Debian 9. After installation I deleted the default database cluster and removed all services auto-starting that cluster. I've now ran initdb to initialize a new cluster in a custom location and written and enabled a systemd .service file to auto run that.

I am running into a problem with the service successfully starting when the machine is started, but then the database instantly shutting down and the service stopping immediately after. The same occurs when I manually start the service using systemctl.

.service file:

[Unit]
Description=PostgreSQL database server
Documentation=man:postgres(1)

[Service]
Environment=PGDATA=/home/(user)/.db
Environment=PGLOG=/home/(user)/postgres.log
Environment=PGSTARTTIMEOUT=270

Type=notify
User=(user)
Group=(group)
ExecStart=/usr/lib/postgresql/11/bin/pg_ctl start -D ${PGDATA} -l ${PGLOG} -t ${PGSTARTTIMEOUT}
ExecStop=/usr/lib/postgresql/11/bin/pg_ctl stop -D ${PGDATA} -m fast
ExecReload=/usr/lib/postgresql/11/bin/pg_ctl reload -D ${PGDATA}
KillMode=mixed
KillSignal=SIGINT
TimeoutSec=300

[Install]
WantedBy=multi-user.target

Command used to start cluster manually:

su (user) -c '/usr/lib/postgresql/11/bin/pg_ctl start -D /home/(user)/.db -l /home/(user)/postgres.log -t 270'

PostgreSQL log when starting via systemctl:

2019-01-13 11:36:16.506 EST [1469] LOG: listening on IPv4 address "127.0.0.1", port 5432

2019-01-13 11:36:16.508 EST [1469] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"

2019-01-13 11:36:16.530 EST [1470] LOG: database system was shut down at 2019 01-13 11:17:07 EST

2019-01-13 11:36:16.537 EST [1469] LOG: database system is ready to accept connections

2019-01-13 11:36:16.595 EST [1469] LOG: received fast shutdown request

2019-01-13 11:36:16.600 EST [1469] LOG: aborting any active transactions

2019-01-13 11:36:16.605 EST [1469] LOG: background worker "logical replication launcher" (PID 1476) exited with exit code 1

2019-01-13 11:36:16.605 EST [1471] LOG: shutting down

2019-01-13 11:36:16.625 EST [1469] LOG: database system is shut down

journalctl | grep postgres:

Jan 13 11:42:01 vps76296 systemd[1]: Starting PostgreSQL database server…

Jan 13 11:42:01 vps76296 systemd[1]: Starting PostgreSQL RDBMS…

Jan 13 11:42:02 vps76296 systemd[1]: Started PostgreSQL RDBMS.

Jan 13 11:42:02 vps76296 systemd[1]: postgres-start.service: Got notification
message from PID 749, but reception only permitted for main PID 729

Jan 13 11:42:02 vps76296 pg_ctl[729]: waiting for server to start…. done

Jan 13 11:42:02 vps76296 pg_ctl[729]: server started

Jan 13 11:42:02 vps76296 pg_ctl[761]: waiting for server to shut down…. done

Jan 13 11:42:02 vps76296 pg_ctl[761]: server stopped

Jan 13 11:42:02 vps76296 systemd[1]: Started PostgreSQL database server.

strace log is also available upon request. It's really long so I opted not to include it unless needed.

Why is my service instantly stopping instantly after starting?

Best Answer

That Type=notify makes me suspicious here. According to systemd docs: "… similar to exec; however, it is expected that the service sends a notification message via sd_notify(3) …"

And pg_ctl isn't a daemon itself but its controlling utility rather which would exit immediately.

I'd suggest using different Typeoneshot.

Also pay attention to systemctl status …serviceName… output — it might have explanations.