From d12206817f78721b7739389c23277f959a61c263 Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Fri, 21 Feb 2025 13:22:37 +0900
Subject: [PATCH] pg_recvlogical: accept if -d is not specified

---
 src/bin/pg_basebackup/pg_recvlogical.c        | 20 ++++++++++++-------
 src/bin/pg_basebackup/t/030_pg_recvlogical.pl |  4 ++--
 2 files changed, 15 insertions(+), 9 deletions(-)

diff --git a/src/bin/pg_basebackup/pg_recvlogical.c b/src/bin/pg_basebackup/pg_recvlogical.c
index b9ea23e142..3fbc2e819e 100644
--- a/src/bin/pg_basebackup/pg_recvlogical.c
+++ b/src/bin/pg_basebackup/pg_recvlogical.c
@@ -20,6 +20,7 @@
 
 #include "common/file_perm.h"
 #include "common/logging.h"
+#include "common/username.h"
 #include "fe_utils/option_utils.h"
 #include "getopt_long.h"
 #include "libpq-fe.h"
@@ -864,6 +865,18 @@ main(int argc, char **argv)
 		exit(1);
 	}
 
+	if (dbname == NULL)
+	{
+		if (getenv("PGDATABASE"))
+			dbname = getenv("PGDATABASE");
+		else if (dbuser)
+			dbname = dbuser;
+		else if (getenv("PGUSER"))
+			dbname = getenv("PGUSER");
+		else
+			dbname = (char *) get_user_name_or_exit(progname);
+	}
+
 	/*
 	 * Required arguments
 	 */
@@ -881,13 +894,6 @@ main(int argc, char **argv)
 		exit(1);
 	}
 
-	if (!do_drop_slot && dbname == NULL)
-	{
-		pg_log_error("no database specified");
-		pg_log_error_hint("Try \"%s --help\" for more information.", progname);
-		exit(1);
-	}
-
 	if (!do_drop_slot && !do_create_slot && !do_start_slot)
 	{
 		pg_log_error("at least one action needs to be specified");
diff --git a/src/bin/pg_basebackup/t/030_pg_recvlogical.pl b/src/bin/pg_basebackup/t/030_pg_recvlogical.pl
index a6e1060016..6fccdba63c 100644
--- a/src/bin/pg_basebackup/t/030_pg_recvlogical.pl
+++ b/src/bin/pg_basebackup/t/030_pg_recvlogical.pl
@@ -29,8 +29,8 @@ $node->start;
 
 $node->command_fails(['pg_recvlogical'], 'pg_recvlogical needs a slot name');
 $node->command_fails(
-	[ 'pg_recvlogical', '--slot' => 'test' ],
-	'pg_recvlogical needs a database');
+	[ 'pg_recvlogical', '--slot' => 'test', '--user' => 'postgres' ],
+	'pg_recvlogical needs an action');
 $node->command_fails(
 	[ 'pg_recvlogical', '--slot' => 'test', '--dbname' => 'postgres' ],
 	'pg_recvlogical needs an action');
-- 
2.43.5

