diff --git a/contrib/pg_upgrade/test.sh b/contrib/pg_upgrade/test.sh
index 126d5d16066e22ba55cc1e28a00e4087b8056b70..5062ff0ea67b13600e0fa52abecbd04be9ac170f 100644
--- a/contrib/pg_upgrade/test.sh
+++ b/contrib/pg_upgrade/test.sh
@@ -84,14 +84,6 @@ if [ "$1" = '--install' ]; then
 	LIBPATH=$libdir:$LIBPATH
 	export LIBPATH
 	PATH=$libdir:$PATH
-
-	# We need to make it use psql from our temporary installation,
-	# because otherwise the installcheck run below would try to
-	# use psql from the proper installation directory, which might
-	# be outdated or missing. But don't override anything else that's
-	# already in EXTRA_REGRESS_OPTS.
-	EXTRA_REGRESS_OPTS="$EXTRA_REGRESS_OPTS --psqldir=$bindir"
-	export EXTRA_REGRESS_OPTS
 fi
 
 : ${oldbindir=$bindir}
@@ -100,6 +92,14 @@ fi
 oldsrc=`cd "$oldsrc" && pwd`
 newsrc=`cd ../.. && pwd`
 
+# We need to make pg_regress use psql from the desired installation
+# (likely a temporary one), because otherwise the installcheck run
+# below would try to use psql from the proper installation directory
+# of the target version, which might be outdated or not exist. But
+# don't override anything else that's already in EXTRA_REGRESS_OPTS.
+EXTRA_REGRESS_OPTS="$EXTRA_REGRESS_OPTS --psqldir='$oldbindir'"
+export EXTRA_REGRESS_OPTS
+
 PATH=$bindir:$PATH
 export PATH