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