diff --git a/src/bin/psql/mainloop.c b/src/bin/psql/mainloop.c
index f7b1b94..0b75a76 100644
--- a/src/bin/psql/mainloop.c
+++ b/src/bin/psql/mainloop.c
@@ -231,11 +231,16 @@ MainLoop(FILE *source)
 		/* Recognize "help", "quit", "exit" only in interactive mode */
 		if (pset.cur_cmd_interactive)
 		{
-			char	   *first_word = line;
+			char	   *first_word = NULL;
 			char	   *rest_of_line = NULL;
 			bool		found_help = false;
 			bool		found_exit_or_quit = false;
 			bool		found_q = false;
+			unsigned int offset = 0;
+
+			/* Ignore leading whitespace */
+			offset = strspn(line, " \t");
+			first_word = line + offset;
 
 			/* Search for the words we recognize;  must be first word */
 			if (pg_strncasecmp(first_word, "help", 4) == 0)
