summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--postgresql-setup.in459
1 files changed, 235 insertions, 224 deletions
diff --git a/postgresql-setup.in b/postgresql-setup.in
index 65f540a..1c66a2d 100644
--- a/postgresql-setup.in
+++ b/postgresql-setup.in
@@ -71,6 +71,241 @@ warn() { echo >&2 $"WARNING: $@" ; }
info() { echo >&2 $" * $@" ; }
debug() { test "$option_debug" = "1" && echo >&2 $"DEBUG: $@"; }
+
+# code shared between initdb and upgrade actions
+perform_initdb()
+{
+ if [ ! -e "$pgdata" ]; then
+ mkdir "$pgdata" || return 1
+ chown postgres:postgres "$pgdata"
+ chmod go-rwx "$pgdata"
+ fi
+
+ # Clean up SELinux tagging for pgdata
+ [ -x /sbin/restorecon ] && /sbin/restorecon "$pgdata"
+
+ # Create the initdb log file if needed
+ if [ ! -e "$PGLOG" -a ! -h "$PGLOG" ]; then
+ touch "$PGLOG" || return 1
+ chown postgres:postgres "$PGLOG"
+ chmod go-rwx "$PGLOG"
+ [ -x /sbin/restorecon ] && /sbin/restorecon "$PGLOG"
+ fi
+
+ # Initialize the database
+ initdbcmd="$PGENGINE/initdb --pgdata='$pgdata' --auth='ident'"
+ initdbcmd+=" $PGSETUP_INITDB_OPTIONS"
+
+ $SU -l postgres -c "$initdbcmd" >> "$PGLOG" 2>&1 < /dev/null
+
+ # Create directory for postmaster log files
+ mkdir "$pgdata/pg_log"
+ chown postgres:postgres "$pgdata/pg_log"
+ chmod go-rwx "$pgdata/pg_log"
+ [ -x /sbin/restorecon ] && /sbin/restorecon "$pgdata/pg_log"
+
+ local pgconf="$pgdata/postgresql.conf"
+ sed -i "s|^[[:space:]#]*port[[:space:]]=[^#]*|port = $pgport |g" \
+ "$pgconf" \
+ && grep "^port = " "$pgconf" >/dev/null
+
+ test $? -ne 0 && {
+ error "can not change port in $pgdata/postgresql.conf"
+ return 1
+ }
+
+ if [ -f "$pgdata/PG_VERSION" ]; then
+ return 0
+ fi
+
+ return 1
+}
+
+
+initdb()
+{
+ if [ -f "$pgdata/PG_VERSION" ]; then
+ error $"Data directory $pgdata is not empty!"
+ script_result=1
+ else
+ info $"Initializing database in $pgdata."
+ if perform_initdb; then
+ info $"Initialized."
+ else
+ error $"Initializing database failed, see $PGLOG"
+ script_result=1
+ fi
+ fi
+}
+
+
+upgrade()
+{
+ # must see previous version in PG_VERSION
+ if [ ! -f "$pgdata/PG_VERSION" -o \
+ x`cat "$pgdata/PG_VERSION"` != x"$PREVMAJORVERSION" ]
+ then
+ error $"Cannot upgrade because the database in $pgdata is not of"
+ error_q $"compatible previous version $PREVMAJORVERSION."
+ exit 1
+ fi
+ if [ ! -x "$PGENGINE/pg_upgrade" ]; then
+ echo
+ echo $"Please install the postgresql-upgrade RPM."
+ echo
+ exit 5
+ fi
+
+ # Set up log file for pg_upgrade
+ rm -f "$PGUPLOG"
+ touch "$PGUPLOG" || exit 1
+ chown postgres:postgres "$PGUPLOG"
+ chmod go-rwx "$PGUPLOG"
+ [ -x /sbin/restorecon ] && /sbin/restorecon "$PGUPLOG"
+
+ # Move old DB to pgdataold
+ pgdataold="${pgdata}-old"
+ rm -rf "$pgdataold"
+ mv "$pgdata" "$pgdataold" || exit 1
+
+ # Create configuration file for upgrade process
+ HBA_CONF_BACKUP="$pgdataold/pg_hba.conf.postgresql-setup.`date +%s`"
+ HBA_CONF_BACKUP_EXISTS=0
+
+ if [ ! -f $HBA_CONF_BACKUP ]; then
+ mv "$pgdataold/pg_hba.conf" "$HBA_CONF_BACKUP"
+ HBA_CONF_BACKUP_EXISTS=1
+
+ # For fluent upgrade 'postgres' user should be able to connect
+ # to any database without password. Temporarily, no other type
+ # of connection is needed.
+ echo "local all postgres ident" > "$pgdataold/pg_hba.conf"
+ fi
+
+ echo -n $"Upgrading database: "
+
+ # Create empty new-format database
+ if perform_initdb; then
+ # Do the upgrade
+ $SU -l postgres -c "$PGENGINE/pg_upgrade \
+ '--old-bindir=$PREVPGENGINE' \
+ '--new-bindir=$PGENGINE' \
+ '--old-datadir=$pgdataold' \
+ '--new-datadir=$pgdata' \
+ --link \
+ '--old-port=$PGPORT' '--new-port=$PGPORT' \
+ --user=postgres \
+ $PGSETUP_PGUPGRADE_OPTIONS" \
+ >> "$PGUPLOG" 2>&1 < /dev/null
+ if [ $? -ne 0 ]; then
+ # pg_upgrade failed
+ script_result=1
+ fi
+ else
+ # initdb failed
+ script_result=1
+ fi
+
+ # Move back the backed-up pg_hba.conf regardless of the script_result.
+ if [ x$HBA_CONF_BACKUP_EXISTS = x1 ]; then
+ mv -f "$HBA_CONF_BACKUP" "$pgdataold/pg_hba.conf"
+ fi
+
+ if [ $script_result -eq 0 ]; then
+ echo $"OK"
+ echo
+ echo $"The configuration files were replaced by default configuration."
+ echo $"The previous configuration and data are stored in folder"
+ echo $pgdataold.
+ else
+ # Clean up after failure
+ rm -rf "$pgdata"
+ mv "$pgdataold" "$pgdata"
+ echo $"failed"
+ fi
+ echo
+ echo $"See $PGUPLOG for details."
+}
+
+
+handle_sysconfig()
+{
+ local mode="$1"
+ local service="$2"
+ local sysconfig_file="$SYSCONFIG_DIR/$service"
+
+ test -r "$sysconfig_file" || {
+ warn "system config file '$sysconfig_file' not found or unreadable"
+ return 1
+ }
+
+ unset PGPORT PGDATA
+ . "$sysconfig_file"
+ sysconfig_pgdata="$PGDATA"
+ sysconfig_pgport="$PGPORT"
+ unset PGPORT PGDATA
+
+ test -n "$sysconfig_pgdata" && debug "sysconfig pgdata: '$sysconfig_pgdata'"
+ test -n "$sysconfig_pgport" && debug "sysconfig pgport: $sysconfig_pgport"
+}
+
+
+# This is mostly for backward compatibility with version postgresql-setup from
+# postgresql package <= 9.3.4-7 as this type of configuration is not adviced
+# anymore. But user still may override the /etc/sysconfig/* settings with
+# Environment= statement in service file. Note that this parsing technique
+# fails for PGDATA pathnames containing spaces, but there's not much we can do
+# about it given systemctl's output format.
+
+handle_service_file()
+{
+ local mode="$1"
+ local service="$2"
+
+ local systemd_env="$(systemctl show -p Environment "${service}.service")" \
+ || { return; }
+
+ for env_var in `echo "$systemd_env" | sed 's/^Environment=//'`; do
+ # If one variable name is defined multiple times the last definition wins.
+ case "$env_var" in
+ PGDATA=*)
+ unit_pgdata="${env_var##PGDATA=}"
+ debug "unit's datadir: '$unit_pgdata'"
+ ;;
+ PGPORT=*)
+ unit_pgport="${env_var##PGPORT=}"
+ debug "unit's pgport: $unit_pgport"
+ ;;
+ esac
+ done
+}
+
+
+handle_pgconf()
+{
+ local mode="$1"
+ local datadir="$2"
+ local conffile="$datadir/postgresql.conf"
+
+ test "$mode" = initdb && return 0
+
+ debug "postgresql.conf: $conffile"
+
+ test -r "$conffile" || {
+ error "config file $conffile is not readable or does not exist"
+ return 1
+ }
+
+ local sp='[[:space:]]'
+ local sed_expr="s/^$sp*port$sp*=$sp\([0-9]\+\).*/\1/p"
+
+ rv=0
+ conf_pgport=`sed -n "$sed_expr" $conffile | tail -1` || rv=1
+ test -n "$conf_pgport" && debug "postgresql.conf pgport: $conf_pgport"
+ return $rv
+}
+
+
# <Compat>
# Alow users to use the old style arguments like
# 'postgresql-setup initdb $SERVICE_NAME'.
@@ -198,80 +433,6 @@ debug "mode used: $option_mode"
debug "service name: $option_service"
debug "port: $pgport"
-handle_sysconfig()
-{
- local mode="$1"
- local service="$2"
- local sysconfig_file="$SYSCONFIG_DIR/$service"
-
- test -r "$sysconfig_file" || {
- warn "system config file '$sysconfig_file' not found or unreadable"
- return 1
- }
-
- unset PGPORT PGDATA
- . "$sysconfig_file"
- sysconfig_pgdata="$PGDATA"
- sysconfig_pgport="$PGPORT"
- unset PGPORT PGDATA
-
- test -n "$sysconfig_pgdata" && debug "sysconfig pgdata: '$sysconfig_pgdata'"
- test -n "$sysconfig_pgport" && debug "sysconfig pgport: $sysconfig_pgport"
-}
-
-# This is mostly for backward compatibility with version <= 9.3.4-7 as this type
-# of configuration is not adviced anymore. But user still may override the
-# /etc/sysconfig/* settings with Environment= statement in service file. Note
-# that this parsing technique fails for PGDATA pathnames containing spaces, but
-# there's not much we can do about it given systemctl's output format.
-
-handle_service_file()
-{
- local mode="$1"
- local service="$2"
-
- local systemd_env="$(systemctl show -p Environment "${service}.service")" \
- || { return; }
-
- for env_var in `echo "$systemd_env" | sed 's/^Environment=//'`; do
- # If one variable name is defined multiple times the last definition wins.
- case "$env_var" in
- PGDATA=*)
- unit_pgdata="${env_var##PGDATA=}"
- debug "unit's datadir: '$unit_pgdata'"
- ;;
- PGPORT=*)
- unit_pgport="${env_var##PGPORT=}"
- debug "unit's pgport: $unit_pgport"
- ;;
- esac
- done
-}
-
-handle_pgconf()
-{
- local mode="$1"
- local datadir="$2"
- local conffile="$datadir/postgresql.conf"
-
- test "$mode" = initdb && return 0
-
- debug "postgresql.conf: $conffile"
-
- test -r "$conffile" || {
- error "config file $conffile is not readable or does not exist"
- return 1
- }
-
- local sp='[[:space:]]'
- local sed_expr="s/^$sp*port$sp*=$sp\([0-9]\+\).*/\1/p"
-
- rv=0
- conf_pgport=`sed -n "$sed_expr" $conffile | tail -1` || rv=1
- test -n "$conf_pgport" && debug "postgresql.conf pgport: $conf_pgport"
- return $rv
-}
-
handle_sysconfig "$option_mode" "$option_service"
handle_service_file "$option_mode" "$option_service"
@@ -305,156 +466,6 @@ export PGPORT=$pgport
script_result=0
-# code shared between initdb and upgrade actions
-perform_initdb()
-{
- if [ ! -e "$pgdata" ]; then
- mkdir "$pgdata" || return 1
- chown postgres:postgres "$pgdata"
- chmod go-rwx "$pgdata"
- fi
-
- # Clean up SELinux tagging for pgdata
- [ -x /sbin/restorecon ] && /sbin/restorecon "$pgdata"
-
- # Create the initdb log file if needed
- if [ ! -e "$PGLOG" -a ! -h "$PGLOG" ]; then
- touch "$PGLOG" || return 1
- chown postgres:postgres "$PGLOG"
- chmod go-rwx "$PGLOG"
- [ -x /sbin/restorecon ] && /sbin/restorecon "$PGLOG"
- fi
-
- # Initialize the database
- initdbcmd="$PGENGINE/initdb --pgdata='$pgdata' --auth='ident'"
- initdbcmd+=" $PGSETUP_INITDB_OPTIONS"
-
- $SU -l postgres -c "$initdbcmd" >> "$PGLOG" 2>&1 < /dev/null
-
- # Create directory for postmaster log files
- mkdir "$pgdata/pg_log"
- chown postgres:postgres "$pgdata/pg_log"
- chmod go-rwx "$pgdata/pg_log"
- [ -x /sbin/restorecon ] && /sbin/restorecon "$pgdata/pg_log"
-
- local pgconf="$pgdata/postgresql.conf"
- sed -i "s|^[[:space:]#]*port[[:space:]]=[^#]*|port = $pgport |g" \
- "$pgconf" \
- && grep "^port = " "$pgconf" >/dev/null
-
- test $? -ne 0 && {
- error "can not change port in $pgdata/postgresql.conf"
- return 1
- }
-
- if [ -f "$pgdata/PG_VERSION" ]; then
- return 0
- fi
-
- return 1
-}
-
-initdb(){
- if [ -f "$pgdata/PG_VERSION" ]; then
- error $"Data directory $pgdata is not empty!"
- script_result=1
- else
- info $"Initializing database in $pgdata."
- if perform_initdb; then
- info $"Initialized."
- else
- error $"Initializing database failed, see $PGLOG"
- script_result=1
- fi
- fi
-}
-
-upgrade(){
- # must see previous version in PG_VERSION
- if [ ! -f "$pgdata/PG_VERSION" -o \
- x`cat "$pgdata/PG_VERSION"` != x"$PREVMAJORVERSION" ]
- then
- error $"Cannot upgrade because the database in $pgdata is not of"
- error_q $"compatible previous version $PREVMAJORVERSION."
- exit 1
- fi
- if [ ! -x "$PGENGINE/pg_upgrade" ]; then
- echo
- echo $"Please install the postgresql-upgrade RPM."
- echo
- exit 5
- fi
-
- # Set up log file for pg_upgrade
- rm -f "$PGUPLOG"
- touch "$PGUPLOG" || exit 1
- chown postgres:postgres "$PGUPLOG"
- chmod go-rwx "$PGUPLOG"
- [ -x /sbin/restorecon ] && /sbin/restorecon "$PGUPLOG"
-
- # Move old DB to pgdataold
- pgdataold="${pgdata}-old"
- rm -rf "$pgdataold"
- mv "$pgdata" "$pgdataold" || exit 1
-
- # Create configuration file for upgrade process
- HBA_CONF_BACKUP="$pgdataold/pg_hba.conf.postgresql-setup.`date +%s`"
- HBA_CONF_BACKUP_EXISTS=0
-
- if [ ! -f $HBA_CONF_BACKUP ]; then
- mv "$pgdataold/pg_hba.conf" "$HBA_CONF_BACKUP"
- HBA_CONF_BACKUP_EXISTS=1
-
- # For fluent upgrade 'postgres' user should be able to connect
- # to any database without password. Temporarily, no other type
- # of connection is needed.
- echo "local all postgres ident" > "$pgdataold/pg_hba.conf"
- fi
-
- echo -n $"Upgrading database: "
-
- # Create empty new-format database
- if perform_initdb; then
- # Do the upgrade
- $SU -l postgres -c "$PGENGINE/pg_upgrade \
- '--old-bindir=$PREVPGENGINE' \
- '--new-bindir=$PGENGINE' \
- '--old-datadir=$pgdataold' \
- '--new-datadir=$pgdata' \
- --link \
- '--old-port=$PGPORT' '--new-port=$PGPORT' \
- --user=postgres \
- $PGSETUP_PGUPGRADE_OPTIONS" \
- >> "$PGUPLOG" 2>&1 < /dev/null
- if [ $? -ne 0 ]; then
- # pg_upgrade failed
- script_result=1
- fi
- else
- # initdb failed
- script_result=1
- fi
-
- # Move back the backed-up pg_hba.conf regardless of the script_result.
- if [ x$HBA_CONF_BACKUP_EXISTS = x1 ]; then
- mv -f "$HBA_CONF_BACKUP" "$pgdataold/pg_hba.conf"
- fi
-
- if [ $script_result -eq 0 ]; then
- echo $"OK"
- echo
- echo $"The configuration files were replaced by default configuration."
- echo $"The previous configuration and data are stored in folder"
- echo $pgdataold.
- else
- # Clean up after failure
- rm -rf "$pgdata"
- mv "$pgdataold" "$pgdata"
- echo $"failed"
- fi
- echo
- echo $"See $PGUPLOG for details."
-}
# See how we were called.
case "$option_mode" in