Blame SOURCES/postgresql-setup

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