diff --git a/src/bin/pg_rewind/RewindTest.pm b/src/bin/pg_rewind/RewindTest.pm
index 76ce295cef..e6041f38a5 100644
--- a/src/bin/pg_rewind/RewindTest.pm
+++ b/src/bin/pg_rewind/RewindTest.pm
@@ -119,6 +119,11 @@ sub setup_cluster
 	# Initialize master, data checksums are mandatory
 	$node_master = get_new_node('master' . ($extra_name ? "_${extra_name}" : ''));
 	$node_master->init(allows_streaming => 1);
+	# Set wal_keep_segments to prevent WAL segment recycling after enforced
+	# checkpoints in the tests.
+	$node_master->append_conf('postgresql.conf', qq(
+wal_keep_segments = 20
+));
 }
 
 sub start_master
diff --git a/src/test/perl/PostgresNode.pm b/src/test/perl/PostgresNode.pm
index edcac6fb9f..988942f547 100644
--- a/src/test/perl/PostgresNode.pm
+++ b/src/test/perl/PostgresNode.pm
@@ -434,7 +434,6 @@ sub init
 		}
 		print $conf "max_wal_senders = 5\n";
 		print $conf "max_replication_slots = 5\n";
-		print $conf "wal_keep_segments = 20\n";
 		print $conf "max_wal_size = 128MB\n";
 		print $conf "shared_buffers = 1MB\n";
 		print $conf "wal_log_hints = on\n";
