#!/usr/bin/env bash # bash strict mode http://redsymbol.net/articles/unofficial-bash-strict-mode/ set -euo pipefail IFS=$'\n\t' # 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. # PGMAJORVERSION is major version PGMAJORVERSION=9.4 # PREVMAJORVERSION is the previous major version, e.g., 8.4, for upgrades PREVMAJORVERSION=9.3 if [ ! -z "$1" ]; then PGDATA=$1 fi if [ ! -d "$PGDATA" ]; then echo "Usage: $0 database-path" exit 1 fi export PGDATA # Check for the PGDATA structure if [ ! -f "$PGDATA/PG_VERSION" ] || [ ! -d "$PGDATA/base" ]; then # No existing PGDATA! Warn the user to initdb it. cat <<-EOF $PGDATA is missing or empty. Use a command like sudo -u postgres initdb --locale en_US.UTF-8 -D '$PGDATA' with relevant options, to initialize the database cluster. EOF exit 1 fi current_version=$(cat "$PGDATA/PG_VERSION") case $current_version in $PGMAJORVERSION) ;; $PREVMAJORVERSION) cat <<-EOF An old version of the database format was found. See https://wiki.archlinux.org/index.php/PostgreSQL#Upgrading_PostgreSQL EOF exit 1 ;; *) cat <<-EOF An old version of the database format was found. You need to dump and reload before using PostgreSQL $PGMAJORVERSION. See http://www.postgresql.org/docs/$PGMAJORVERSION/static/upgrading.html EOF exit 1 esac # Tabs are significant for `<<-` # vim: noexpandtab