diff --git a/src/bin/pg_upgrade/t/002_pg_upgrade.pl b/src/bin/pg_upgrade/t/002_pg_upgrade.pl
index 67e0be6856..8dbda0950e 100644
--- a/src/bin/pg_upgrade/t/002_pg_upgrade.pl
+++ b/src/bin/pg_upgrade/t/002_pg_upgrade.pl
@@ -216,6 +216,9 @@ chdir ${PostgreSQL::Test::Utils::tmp_check};
 # Upgrade the instance.
 $oldnode->stop;
 
+# Use a short path for socket directories.
+my $socketdir = PostgreSQL::Test::Utils::tempdir_short;
+
 # Cause a failure at the start of pg_upgrade, this should create the logging
 # directory pg_upgrade_output.d but leave it around.  Keep --check for an
 # early exit.
@@ -228,6 +231,7 @@ command_fails(
 		'-B',         $newbindir,
 		'-p',         $oldnode->port,
 		'-P',         $newnode->port,
+		'-s',         $socketdir,
 		'--check'
 	],
 	'run of pg_upgrade --check for new instance with incorrect binary path');
@@ -241,7 +245,8 @@ command_ok(
 		'pg_upgrade', '--no-sync',        '-d', $oldnode->data_dir,
 		'-D',         $newnode->data_dir, '-b', $oldbindir,
 		'-B',         $newbindir,         '-p', $oldnode->port,
-		'-P',         $newnode->port,     '--check'
+		'-P',         $newnode->port,     '-s', $socketdir,
+		'--check'
 	],
 	'run of pg_upgrade --check for new instance');
 ok(!-d $newnode->data_dir . "/pg_upgrade_output.d",
@@ -253,7 +258,7 @@ command_ok(
 		'pg_upgrade', '--no-sync',        '-d', $oldnode->data_dir,
 		'-D',         $newnode->data_dir, '-b', $oldbindir,
 		'-B',         $newbindir,         '-p', $oldnode->port,
-		'-P',         $newnode->port
+		'-P',         $newnode->port,     '-s', $socketdir,
 	],
 	'run of pg_upgrade for new instance');
 ok( !-d $newnode->data_dir . "/pg_upgrade_output.d",
