Skip to content

Commit 3df8d4b

Browse files
committed
Block execution with custom pgdata entry in conf file
When data_directory entry in config file is detected. According to postgresql docs: > Notice that data_directory overrides -D and PGDATA for the location of the data directory, but not for the location of the configuration files. That means that detected PGDATA from .service file is incorrect and process should not continue. Currently postgresql-{setup, upgrade} does not take into consideration data_directory entry in configuration file. Detecting valid data_directory regardless of value should abort execution. Resolves: #1935301
1 parent c67a5ab commit 3df8d4b

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

NEWS

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
New in 8.6 version:
44

5-
*
5+
* Block execution when 'data_directory' in configuration file is detected.
6+
Currently postgresql-{setup, upgrade} doesn't support custom data directory.
7+
All changes connected with 'data_directory' made in .conf or .service file
8+
should be reverted before usage.
9+
That means that detected PGDATA variable in .service file and 'data_directory'
10+
in .conf file should be restored to default values.
611

712
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
813

bin/postgresql-setup.in

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -460,12 +460,13 @@ handle_pgconf()
460460
}
461461

462462
local sp='[[:space:]]'
463-
local sed_expr="s/^$sp*port$sp*=$sp\([0-9]\+\).*/\1/p"
463+
local sed_expr_port="s/^$sp*port$sp*=$sp*\([0-9]\+\).*/\1/p"
464+
local sed_expr_pgdata="s/^$sp*data_directory$sp*=\(.*\)/\1/p"
464465

465-
rv=0
466-
conf_pgport=`sed -n "$sed_expr" $conffile | tail -1` || rv=1
466+
conf_pgport=`sed -n "$sed_expr_port" $conffile | tail -1`
467+
conf_pgdata=`sed -n "$sed_expr_pgdata" $conffile | tail -1`
467468
test -n "$conf_pgport" && debug "postgresql.conf pgport: $conf_pgport"
468-
return $rv
469+
test -n "$conf_pgdata" && debug "postgresql.conf pgdata (data_directory): $conf_pgdata"
469470
}
470471

471472

@@ -737,6 +738,19 @@ if test upgrade = "$option_mode"; then
737738
&& warn "Old pgport $pgport has bigger priority than --pgport value."
738739
fi
739740

741+
# Check for data_directory entry in config file
742+
# valid entry means that custom PGDATA path is present which is not supported
743+
# BZ (#1935301)
744+
if test -n "$conf_pgdata"; then
745+
error $"data_directory field in configuration file is not supported."
746+
error_q $"db datadir (PGDATA) needs to be specified exclusively in service/unit"
747+
error_q $"file as an Environment variable."
748+
error_q $"In order to use this script, please remove data_directory entry from"
749+
error_q $"configuration file and make sure that the default location"
750+
error_q $"(PGDATA) in .service file is valid."
751+
exit 1
752+
fi
753+
740754
# We expect that for upgrade - the previous stack was in working state (thus
741755
# running on the default port).
742756
test "$option_mode" = upgrade -a "$pgport" = default \

bin/postgresql-upgrade.in

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,30 @@ if [ ! -x "$upgradefrom_engine/postgres" ]; then
163163
exit 1
164164
fi
165165

166+
# Check for data_directory entry in config file
167+
# valid entry means that custom PGDATA path is present which is not supported
168+
# BZ (#1935301)
169+
conffile="$option_datadir/postgresql.conf"
170+
171+
test -r "$conffile" || {
172+
error "config file $conffile is not readable or does not exist"
173+
die "Old cluster in '$data' does not seem to be initialized"
174+
}
175+
176+
sp='[[:space:]]'
177+
sed_expr_pgdata="s/^$sp*data_directory$sp*=\(.*\)/\1/p"
178+
conf_pgdata=`sed -n "$sed_expr_pgdata" $conffile | tail -1`
179+
180+
test -n "$conf_pgdata" && {
181+
error $"data_directory field in configuration file ($conffile) is not supported."
182+
error_q $"db datadir (PGDATA) cannot be specified in the configuration file."
183+
error_q $"In order to use this script for upgrade, please, move the files"
184+
error_q $"to the default location first and remove data_directory entry from"
185+
error_q $"configuration file."
186+
187+
exit 1
188+
}
189+
166190
exit_handler_revert_datadir=false
167191

168192
exit_handler ()

0 commit comments

Comments
 (0)