diff --git a/src/bin/pg_dump/pg_dumpall.c b/src/bin/pg_dump/pg_dumpall.c
index 9de608d406ce361d506561e6f9152cd9f4ea7013..0c4bad7e06185a3cb65de6e9193b9e7ad131ce87 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
  *
  *
- * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.9 2002/10/18 22:05:36 petere Exp $
+ * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.10 2002/11/22 03:09:43 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -589,10 +589,23 @@ static int
 runPgDump(const char *dbname)
 {
 	PQExpBuffer cmd = createPQExpBuffer();
+	const char *p;
 	int			ret;
 
-	appendPQExpBuffer(cmd, "%s %s -X use-set-session-authorization -Fp %s",
-					  pgdumploc, pgdumpopts->data, dbname);
+	appendPQExpBuffer(cmd, "%s %s -X use-set-session-authorization -Fp '",
+					  pgdumploc, pgdumpopts->data);
+
+	/* Shell quoting is not quite like SQL quoting, so can't use fmtId */
+	for (p = dbname; *p; p++)
+	{
+		if (*p == '\'')
+			appendPQExpBuffer(cmd, "'\"'\"'");
+		else
+			appendPQExpBufferChar(cmd, *p);
+	}
+
+	appendPQExpBufferChar(cmd, '\'');
+
 	if (verbose)
 		fprintf(stderr, _("%s: running %s\n"), progname, cmd->data);
 
@@ -600,6 +613,7 @@ runPgDump(const char *dbname)
 	fflush(stderr);
 
 	ret = system(cmd->data);
+
 	destroyPQExpBuffer(cmd);
 
 	return ret;