diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml
index 2410bee..a9b0732 100644
--- a/doc/src/sgml/ref/psql-ref.sgml
+++ b/doc/src/sgml/ref/psql-ref.sgml
@@ -2616,6 +2616,23 @@ lo_import 152801
+
+ \quit_if string
+
+
+ Quits the psql program if the string evaluates to true.
+
+
+
+
+
+ \quit_unless string
+
+
+ Quits the psql program unless the string evaluates to true.
+
+
+
\qecho text [ ... ]
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index a9a2fdb..7b46fbd 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -1251,6 +1251,29 @@ exec_command(const char *cmd,
else if (strcmp(cmd, "q") == 0 || strcmp(cmd, "quit") == 0)
status = PSQL_CMD_TERMINATE;
+ /* \quit_if */
+ else if (strcmp(cmd, "quit_if") == 0)
+ {
+ char *opt = psql_scan_slash_option(scan_state,
+ OT_NORMAL, NULL, false);
+
+ if (opt)
+ if (ParseVariableBool(opt, "\\quit_if"))
+ status = PSQL_CMD_TERMINATE;
+ }
+
+ /* \quit_unless */
+ else if (strcmp(cmd, "quit_unless") == 0)
+ {
+ char *opt = psql_scan_slash_option(scan_state,
+ OT_NORMAL, NULL, false);
+
+ if (!opt)
+ status = PSQL_CMD_TERMINATE;
+ else if (! ParseVariableBool(opt, "\\quit_unless"))
+ status = PSQL_CMD_TERMINATE;
+ }
+
/* reset(clear) the buffer */
else if (strcmp(cmd, "r") == 0 || strcmp(cmd, "reset") == 0)
{
diff --git a/src/bin/psql/help.c b/src/bin/psql/help.c
index a69c4dd..db29650 100644
--- a/src/bin/psql/help.c
+++ b/src/bin/psql/help.c
@@ -177,6 +177,8 @@ slashUsage(unsigned short int pager)
fprintf(output, _(" \\gexec execute query, then execute each value in its result\n"));
fprintf(output, _(" \\gset [PREFIX] execute query and store results in psql variables\n"));
fprintf(output, _(" \\q quit psql\n"));
+ fprintf(output, _(" \\quit_if [STRING] quit psql if STRING evaluates to true\n"));
+ fprintf(output, _(" \\quit_unless [STRING] quit psql if STRING does not evaluate to true\n"));
fprintf(output, _(" \\crosstabview [COLUMNS] execute query and display results in crosstab\n"));
fprintf(output, _(" \\watch [SEC] execute query every SEC seconds\n"));
fprintf(output, "\n");