diff --git a/src/test/perl/PostgresNode.pm b/src/test/perl/PostgresNode.pm
index b18c6bf33c65f8314add9cbae9dbde3442254053..7b6ddf25de9c202ad30101fef45599314ad3ad46 100644
--- a/src/test/perl/PostgresNode.pm
+++ b/src/test/perl/PostgresNode.pm
@@ -321,7 +321,8 @@ sub get_new_node
 
 	while ($found == 0)
 	{
-		$port++;
+		# wrap correctly around range end
+		$port = 49152 if ++$port >= 65536;
 		print "# Checking for port $port\n";
 		if (!TestLib::run_log([ 'pg_isready', '-p', $port ]))
 		{