3

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?

  • Who wrote this systemd unit? It does not look normal. – Michael Hampton Jan 13 '19 at 17:42
  • I did, based on docs at https://www.postgresql.org/docs/11/server-start.html and a couple other resources I had found providing systemd+postgres explanations. I'd link those as well, but I can't remember where I found them. – RelicBloodvayne Jan 13 '19 at 18:22
  • That's a little strange. If you really installed a Debian package, it should already include a working systemd unit. Where did the package come from, then? – Michael Hampton Jan 13 '19 at 18:32
  • It's a deb package - apt-get. There wasn't a working .service - it was rather a file in init.d with the comment header for systemctl. – RelicBloodvayne Jan 13 '19 at 19:20

1 Answers1

5

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.

poige
  • 9,171
  • 2
  • 24
  • 50
  • I wonder if your comment about pg_ctl not being a daemon and exiting immediately would be the cause - the .service listed in the docs at postgresql.org/docs/11/server-start.html used postgres instead of pg_ctl. – RelicBloodvayne Jan 13 '19 at 18:28
  • Because of your help, specifically the note about pg_ctl not being a daemon but a controlling utility that would always exit immediately, I was able to figure it out. Settings up my postgresql.conf file properly for logging and then using the 'postgres' command instead of 'pg_ctl' fixed the problem! If you want to edit your answer a little I will accept yours! – RelicBloodvayne Jan 13 '19 at 19:50
  • I see no need to edit my answer. First of all I'm not bonus program enthusiast at all; 2nd — the essence of my reply was enough answer to help solving it. – poige Jan 14 '19 at 03:46