27aa74
#!/bin/sh
27aa74
#
27aa74
# postgresql-setup	Initialization and upgrade operations for PostgreSQL
27aa74
27aa74
# PGVERSION is the full package version, e.g., 9.0.2
27aa74
# Note: the specfile inserts the correct value during package build
27aa74
PGVERSION=xxxx
27aa74
# PGENGINE is the directory containing the postmaster executable
27aa74
# Note: the specfile inserts the correct value during package build
27aa74
PGENGINE=xxxx
27aa74
# PREVMAJORVERSION is the previous major version, e.g., 8.4, for upgrades
27aa74
# Note: the specfile inserts the correct value during package build
27aa74
PREVMAJORVERSION=xxxx
27aa74
# PREVPGENGINE is the directory containing the previous postmaster executable
27aa74
# Note: the specfile inserts the correct value during package build
27aa74
PREVPGENGINE=xxxx
27aa74
27aa74
# Absorb configuration settings from the specified systemd service file,
27aa74
# or the default "postgresql" service if not specified
27aa74
SERVICE_NAME="$2"
27aa74
if [ x"$SERVICE_NAME" = x ]
27aa74
then
27aa74
    SERVICE_NAME=postgresql
27aa74
fi
27aa74
27aa74
# this parsing technique fails for PGDATA pathnames containing spaces,
27aa74
# but there's not much I can do about it given systemctl's output format...
27aa74
PGDATA=`systemctl show -p Environment "${SERVICE_NAME}.service" |
27aa74
  sed 's/^Environment=//' | tr ' ' '\n' |
27aa74
  sed -n 's/^PGDATA=//p' | tail -n 1`
27aa74
27aa74
if [ x"$PGDATA" = x ]
27aa74
then
27aa74
    echo "failed to find PGDATA setting in ${SERVICE_NAME}.service"
27aa74
    exit 1
27aa74
fi
27aa74
27aa74
PGPORT=`systemctl show -p Environment "${SERVICE_NAME}.service" |
27aa74
  sed 's/^Environment=//' | tr ' ' '\n' |
27aa74
  sed -n 's/^PGPORT=//p' | tail -n 1`
27aa74
27aa74
if [ x"$PGPORT" = x ]
27aa74
then
27aa74
    echo "failed to find PGPORT setting in ${SERVICE_NAME}.service"
27aa74
    exit 1
27aa74
fi
27aa74
27aa74
# Log file for initdb
27aa74
PGLOG=/var/lib/pgsql/initdb.log
27aa74
27aa74
# Log file for pg_upgrade
27aa74
PGUPLOG=/var/lib/pgsql/pgupgrade.log
27aa74
27aa74
export PGDATA
27aa74
export PGPORT
27aa74
27aa74
# For SELinux we need to use 'runuser' not 'su'
27aa74
if [ -x /sbin/runuser ]
27aa74
then
27aa74
    SU=runuser
27aa74
else
27aa74
    SU=su
27aa74
fi
27aa74
27aa74
script_result=0
27aa74
27aa74
# code shared between initdb and upgrade actions
27aa74
perform_initdb(){
27aa74
	if [ ! -e "$PGDATA" ]
27aa74
	then
27aa74
		mkdir "$PGDATA" || return 1
27aa74
		chown postgres:postgres "$PGDATA"
27aa74
		chmod go-rwx "$PGDATA"
27aa74
	fi
27aa74
	# Clean up SELinux tagging for PGDATA
27aa74
	[ -x /sbin/restorecon ] && /sbin/restorecon "$PGDATA"
27aa74
27aa74
	# Create the initdb log file if needed
27aa74
	if [ ! -e "$PGLOG" -a ! -h "$PGLOG" ]
27aa74
	then
27aa74
		touch "$PGLOG" || return 1
27aa74
		chown postgres:postgres "$PGLOG"
27aa74
		chmod go-rwx "$PGLOG"
27aa74
		[ -x /sbin/restorecon ] && /sbin/restorecon "$PGLOG"
27aa74
	fi
27aa74
27aa74
	# Initialize the database
27aa74
	$SU -l postgres -c "$PGENGINE/initdb --pgdata='$PGDATA' --auth='ident'" >> "$PGLOG" 2>&1 < /dev/null
27aa74
27aa74
	# Create directory for postmaster log files
27aa74
	mkdir "$PGDATA/pg_log"
27aa74
	chown postgres:postgres "$PGDATA/pg_log"
27aa74
	chmod go-rwx "$PGDATA/pg_log"
27aa74
	[ -x /sbin/restorecon ] && /sbin/restorecon "$PGDATA/pg_log"
27aa74
27aa74
	if [ -f "$PGDATA/PG_VERSION" ]
27aa74
	then
27aa74
	    return 0
27aa74
	fi
27aa74
	return 1
27aa74
}
27aa74
27aa74
initdb(){
27aa74
    if [ -f "$PGDATA/PG_VERSION" ]
27aa74
    then
27aa74
	echo $"Data directory is not empty!"
27aa74
	echo
27aa74
	script_result=1
27aa74
    else
27aa74
	echo -n $"Initializing database ... "
27aa74
	if perform_initdb
27aa74
	then
27aa74
	    echo $"OK"
27aa74
	else
27aa74
	    echo $"failed, see $PGLOG"
27aa74
	    script_result=1
27aa74
	fi
27aa74
	echo
27aa74
    fi
27aa74
}
27aa74
27aa74
upgrade(){
27aa74
    # must see previous version in PG_VERSION
27aa74
    if [ ! -f "$PGDATA/PG_VERSION" -o \
27aa74
	 x`cat "$PGDATA/PG_VERSION"` != x"$PREVMAJORVERSION" ]
27aa74
    then
27aa74
	echo
27aa74
	echo $"Cannot upgrade because database is not of version $PREVMAJORVERSION."
27aa74
	echo
27aa74
	exit 1
27aa74
    fi
27aa74
    if [ ! -x "$PGENGINE/pg_upgrade" ]
27aa74
    then
27aa74
	echo
27aa74
	echo $"Please install the postgresql-upgrade RPM."
27aa74
	echo
27aa74
	exit 5
27aa74
    fi
27aa74
27aa74
    # Make sure service is stopped
27aa74
    # Using service here makes it work both with systemd and other init systems
27aa74
    service "$SERVICE_NAME" stop
27aa74
27aa74
    # Set up log file for pg_upgrade
27aa74
    rm -f "$PGUPLOG"
27aa74
    touch "$PGUPLOG" || exit 1
27aa74
    chown postgres:postgres "$PGUPLOG"
27aa74
    chmod go-rwx "$PGUPLOG"
27aa74
    [ -x /sbin/restorecon ] && /sbin/restorecon "$PGUPLOG"
27aa74
27aa74
    # Move old DB to PGDATAOLD
27aa74
    PGDATAOLD="${PGDATA}-old"
27aa74
    rm -rf "$PGDATAOLD"
27aa74
    mv "$PGDATA" "$PGDATAOLD" || exit 1
27aa74
27aa74
    echo -n $"Upgrading database: "
27aa74
27aa74
    # Create empty new-format database
27aa74
    if perform_initdb
27aa74
    then
27aa74
	# Do the upgrade
27aa74
	$SU -l postgres -c "$PGENGINE/pg_upgrade \
27aa74
		'--old-bindir=$PREVPGENGINE' \
27aa74
		'--new-bindir=$PGENGINE' \
27aa74
		'--old-datadir=$PGDATAOLD' \
27aa74
		'--new-datadir=$PGDATA' \
27aa74
		--link \
27aa74
		'--old-port=$PGPORT' '--new-port=$PGPORT' \
27aa74
		--user=postgres" >> "$PGUPLOG" 2>&1 < /dev/null
27aa74
	if [ $? -ne 0 ]
27aa74
	then
27aa74
	    # pg_upgrade failed
27aa74
	    script_result=1
27aa74
	fi
27aa74
    else
27aa74
	# initdb failed
27aa74
	script_result=1
27aa74
    fi
27aa74
27aa74
    if [ $script_result -eq 0 ]
27aa74
    then
27aa74
	    echo $"OK"
27aa74
    else
27aa74
	    # Clean up after failure
27aa74
	    rm -rf "$PGDATA"
27aa74
	    mv "$PGDATAOLD" "$PGDATA"
27aa74
27aa74
	    echo $"failed"
27aa74
    fi
27aa74
    echo
27aa74
    echo $"See $PGUPLOG for details."
27aa74
}
27aa74
27aa74
# See how we were called.
27aa74
case "$1" in
27aa74
  initdb)
27aa74
	initdb
27aa74
	;;
27aa74
  upgrade)
27aa74
	upgrade
27aa74
	;;
27aa74
  *)
27aa74
	echo $"Usage: $0 {initdb|upgrade} [ service_name ]"
27aa74
	exit 2
27aa74
esac
27aa74
27aa74
exit $script_result