diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index 1fd43b55e17447da2e2a1b745b344e47ad30d604..b137c387283c88babfb4a51a2c3f627f044d09bb 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -25,7 +25,7 @@
  *	http://archives.postgresql.org/pgsql-bugs/2010-02/msg00187.php
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.584 2010/08/03 19:24:04 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.585 2010/08/13 14:38:03 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -480,19 +480,20 @@ main(int argc, char **argv)
 		}
 	}
 
-	if (optind < (argc - 1))
+	/* Get database name from command line */
+	if (optind < argc)
+		dbname = argv[optind++];
+
+	/* Complain if any arguments remain */
+	if (optind < argc)
 	{
 		fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
-				progname, argv[optind + 1]);
+				progname, argv[optind]);
 		fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
 				progname);
 		exit(1);
 	}
 
-	/* Get database name from command line */
-	if (optind < argc)
-		dbname = argv[optind];
-
 	/* --column-inserts implies --inserts */
 	if (column_inserts)
 		dump_inserts = 1;
diff --git a/src/bin/pg_dump/pg_dumpall.c b/src/bin/pg_dump/pg_dumpall.c
index c9cbc8e73f858386fbeb9b4e6a53c0d1cdf34075..3c15fe68c96bee2d06ec1d0c8399d4d364ea3d94 100644
--- a/src/bin/pg_dump/pg_dumpall.c
+++ b/src/bin/pg_dump/pg_dumpall.c
@@ -6,7 +6,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  *
- * $PostgreSQL: pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.137 2010/08/03 19:24:05 tgl Exp $
+ * $PostgreSQL: pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.138 2010/08/13 14:38:04 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -316,24 +316,7 @@ main(int argc, char *argv[])
 		}
 	}
 
-	/* Add long options to the pg_dump argument list */
-	if (binary_upgrade)
-		appendPQExpBuffer(pgdumpopts, " --binary-upgrade");
-	if (column_inserts)
-		appendPQExpBuffer(pgdumpopts, " --column-inserts");
-	if (disable_dollar_quoting)
-		appendPQExpBuffer(pgdumpopts, " --disable-dollar-quoting");
-	if (disable_triggers)
-		appendPQExpBuffer(pgdumpopts, " --disable-triggers");
-	if (inserts)
-		appendPQExpBuffer(pgdumpopts, " --inserts");
-	if (no_tablespaces)
-		appendPQExpBuffer(pgdumpopts, " --no-tablespaces");
-	if (quote_all_identifiers)
-		appendPQExpBuffer(pgdumpopts, " --quote-all-identifiers");
-	if (use_setsessauth)
-		appendPQExpBuffer(pgdumpopts, " --use-set-session-authorization");
-
+	/* Complain if any arguments remain */
 	if (optind < argc)
 	{
 		fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
@@ -371,6 +354,24 @@ main(int argc, char *argv[])
 		exit(1);
 	}
 
+	/* Add long options to the pg_dump argument list */
+	if (binary_upgrade)
+		appendPQExpBuffer(pgdumpopts, " --binary-upgrade");
+	if (column_inserts)
+		appendPQExpBuffer(pgdumpopts, " --column-inserts");
+	if (disable_dollar_quoting)
+		appendPQExpBuffer(pgdumpopts, " --disable-dollar-quoting");
+	if (disable_triggers)
+		appendPQExpBuffer(pgdumpopts, " --disable-triggers");
+	if (inserts)
+		appendPQExpBuffer(pgdumpopts, " --inserts");
+	if (no_tablespaces)
+		appendPQExpBuffer(pgdumpopts, " --no-tablespaces");
+	if (quote_all_identifiers)
+		appendPQExpBuffer(pgdumpopts, " --quote-all-identifiers");
+	if (use_setsessauth)
+		appendPQExpBuffer(pgdumpopts, " --use-set-session-authorization");
+
 	/*
 	 * If there was a database specified on the command line, use that,
 	 * otherwise try to connect to database "postgres", and failing that
diff --git a/src/bin/pg_dump/pg_restore.c b/src/bin/pg_dump/pg_restore.c
index 4927471dce44393a26e216767813d60db3135625..67a24b6e62c1816a8f3d6b608455df9870cd7fa7 100644
--- a/src/bin/pg_dump/pg_restore.c
+++ b/src/bin/pg_dump/pg_restore.c
@@ -34,7 +34,7 @@
  *
  *
  * IDENTIFICATION
- *		$PostgreSQL: pgsql/src/bin/pg_dump/pg_restore.c,v 1.102 2010/05/15 21:41:16 tgl Exp $
+ *		$PostgreSQL: pgsql/src/bin/pg_dump/pg_restore.c,v 1.103 2010/08/13 14:38:04 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -295,11 +295,22 @@ main(int argc, char **argv)
 		}
 	}
 
+	/* Get file name from command line */
 	if (optind < argc)
-		inputFileSpec = argv[optind];
+		inputFileSpec = argv[optind++];
 	else
 		inputFileSpec = NULL;
 
+	/* Complain if any arguments remain */
+	if (optind < argc)
+	{
+		fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
+				progname, argv[optind]);
+		fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
+				progname);
+		exit(1);
+	}
+
 	/* Should get at most one of -d and -f, else user is confused */
 	if (opts->dbname)
 	{