summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--postgresql-check-db-dir57
-rw-r--r--postgresql.service1
2 files changed, 58 insertions, 0 deletions
diff --git a/postgresql-check-db-dir b/postgresql-check-db-dir
new file mode 100644
index 0000000..4266c7a
--- /dev/null
+++ b/postgresql-check-db-dir
@@ -0,0 +1,57 @@
+#!/bin/sh
+
+# This script verifies that the postgresql data directory has been correctly
+# initialized. We do not want to automatically initdb it, because that has
+# a risk of catastrophic failure (ie, overwriting a valuable database) in
+# corner cases, such as a remotely mounted database on a volume that's a
+# bit slow to mount. But we can at least emit a message advising newbies
+# what to do.
+
+PGDATA="$1"
+
+if [ -z "$PGDATA" ]
+then
+ echo "Usage: $0 database-path"
+ exit 1
+fi
+
+# PGVERSION is the full package version, e.g., 9.1.2
+# Note: the specfile inserts the correct value during package build
+PGVERSION=xxxx
+# PGMAJORVERSION is major version, e.g., 9.1 (this should match PG_VERSION)
+PGMAJORVERSION=`echo "$PGVERSION" | sed 's/^\([0-9]*\.[0-9]*\).*$/\1/'`
+# PREVMAJORVERSION is the previous major version, e.g., 8.4, for upgrades
+# Note: the specfile inserts the correct value during package build
+PREVMAJORVERSION=xxxx
+# PGDOCDIR is the directory containing the package's documentation
+# Note: the specfile inserts the correct value during package build
+PGDOCDIR=xxxx
+
+# Check for the PGDATA structure
+if [ -f "$PGDATA/PG_VERSION" ] && [ -d "$PGDATA/base" ]
+then
+ # Check version of existing PGDATA
+ if [ x`cat "$PGDATA/PG_VERSION"` = x"$PGMAJORVERSION" ]
+ then
+ : A-OK
+ elif [ x`cat "$PGDATA/PG_VERSION"` = x"$PREVMAJORVERSION" ]
+ then
+ echo $"An old version of the database format was found."
+ echo $"Use \"postgresql-setup upgrade\" to upgrade to version $PGMAJORVERSION."
+ echo $"See $PGDOCDIR/README.rpm-dist for more information."
+ exit 1
+ else
+ echo $"An old version of the database format was found."
+ echo $"You need to dump and reload before using PostgreSQL $PGMAJORVERSION."
+ echo $"See $PGDOCDIR/README.rpm-dist for more information."
+ exit 1
+ fi
+else
+ # No existing PGDATA! Warn the user to initdb it.
+ echo $"\"$PGDATA\" is missing or empty."
+ echo $"Use \"postgresql-setup initdb\" to initialize the database cluster."
+ echo $"See $PGDOCDIR/README.rpm-dist for more information."
+ exit 1
+fi
+
+exit 0
diff --git a/postgresql.service b/postgresql.service
index 0c733bd..e59ce5c 100644
--- a/postgresql.service
+++ b/postgresql.service
@@ -25,6 +25,7 @@ StandardOutput=syslog
# Disable OOM kill on the postmaster
OOMScoreAdjust=-1000
+ExecStartPre=/usr/bin/postgresql-check-db-dir ${PGDATA}
ExecStart=/usr/bin/pg_ctl start -D ${PGDATA} -s -o "-p ${PGPORT}" -w -t 300
ExecStop=/usr/bin/pg_ctl stop -D ${PGDATA} -s -m fast
ExecReload=/usr/bin/pg_ctl reload -D ${PGDATA} -s