diff options
Diffstat (limited to 'systemd')
-rw-r--r-- | systemd/Makefile | 7 | ||||
-rw-r--r-- | systemd/README.systemd | 16 | ||||
-rwxr-xr-x | systemd/postgresql-generator | 38 | ||||
-rw-r--r-- | systemd/postgresql.service | 15 | ||||
-rw-r--r-- | systemd/postgresql@.service | 40 |
5 files changed, 116 insertions, 0 deletions
diff --git a/systemd/Makefile b/systemd/Makefile new file mode 100644 index 0000000..ace0c2d --- /dev/null +++ b/systemd/Makefile @@ -0,0 +1,7 @@ +install: + install -d $(DESTDIR)/lib/systemd/system-generators/ $(DESTDIR)/lib/systemd/system/ + install postgresql-generator $(DESTDIR)/lib/systemd/system-generators/ + install -m644 postgresql*.service $(DESTDIR)/lib/systemd/system/ + +reload: install + systemctl daemon-reload diff --git a/systemd/README.systemd b/systemd/README.systemd new file mode 100644 index 0000000..a6b8098 --- /dev/null +++ b/systemd/README.systemd @@ -0,0 +1,16 @@ +systemd unit files for PostgreSQL on Debian/Ubuntu +-------------------------------------------------- + +Each cluster is run as a separate service, called postgresql@version/name. +pg_ctlcluster is invoked with --skip-systemctl-redirect. Logging still goes to +/var/log/postgresql. + +There is a parent service called postgresql.service, that starts/stops/restarts/ +reloads all individual services that are configured as "auto" in +/etc/postgresql/*/*/start.conf. + +The link between start.conf and postgresql.service is established by +postgresql-generator, which creates symlinks in +/run/systemd/generator/postgresql.service.wants/. + + -- Christoph Berg <myon@debian.org> Fri, 18 Jul 2014 23:52:09 +0300 diff --git a/systemd/postgresql-generator b/systemd/postgresql-generator new file mode 100755 index 0000000..12e8102 --- /dev/null +++ b/systemd/postgresql-generator @@ -0,0 +1,38 @@ +#!/bin/sh + +# This systemd generator creates dependency symlinks that make all PostgreSQL +# clusters with "auto" in their start.conf file be started/stopped/reloaded +# when postgresql.service is started/stopped/reloaded. + +set -eu + +gendir="$1" +wantdir="$1/postgresql.service.wants" +bindir="/usr/lib/postgresql/" +#redhat# bindir="/usr/pgsql-" +pgservice="/lib/systemd/system/postgresql@.service" + +mkdir -p "$wantdir" + +for conf in /etc/postgresql/*/*/postgresql.conf; do + # abort loop if glob was not expanded (but accept dead symlinks) + if ! test -e "$conf" && ! test -L "$conf"; then continue; fi + + dir="${conf%/*}" + + # evaluate start.conf + if [ -e "$dir/start.conf" ]; then + start=$(sed 's/#.*$//; /^[[:space:]]*$/d; s/^\s*//; s/\s*$//' "$dir/start.conf") + else + start=auto + fi + [ "$start" = "auto" ] || continue + + verdir="${dir%/*}" + version="${verdir##*/}" + test -x "$bindir$version/bin/postgres" || continue # package got removed + cluster="${dir##*/}" + ln -s "$pgservice" "$wantdir/postgresql@$version-$cluster.service" +done + +exit 0 diff --git a/systemd/postgresql.service b/systemd/postgresql.service new file mode 100644 index 0000000..bcaf814 --- /dev/null +++ b/systemd/postgresql.service @@ -0,0 +1,15 @@ +# 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. + +[Unit] +Description=PostgreSQL RDBMS + +[Service] +Type=oneshot +ExecStart=/bin/true +ExecReload=/bin/true +RemainAfterExit=on + +[Install] +WantedBy=multi-user.target diff --git a/systemd/postgresql@.service b/systemd/postgresql@.service new file mode 100644 index 0000000..8eed65c --- /dev/null +++ b/systemd/postgresql@.service @@ -0,0 +1,40 @@ +# 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 +# 0 is the same as infinity, but "infinity" needs systemd 229 +TimeoutStartSec=0 +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 |