Skip to content

Commit b49d871

Browse files
committed
Fix pg_dumpall to do the right thing with "postgres" database, per
Dave Page. Also, cause it to emit rather than ignore any ACL and datconfig options that may be set for these two databases.
1 parent 6f7fc0b commit b49d871

File tree

1 file changed

+32
-28
lines changed

1 file changed

+32
-28
lines changed

src/bin/pg_dump/pg_dumpall.c

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1994, Regents of the University of California
77
*
88
*
9-
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.60 2005/06/21 04:02:32 tgl Exp $
9+
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.61 2005/06/21 15:22:18 tgl Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -303,7 +303,7 @@ main(int argc, char *argv[])
303303
if (verbose)
304304
dumpTimestamp("Started on");
305305

306-
printf("\\connect \"postgres\"\n\n");
306+
printf("\\connect postgres\n\n");
307307

308308
if (!data_only)
309309
{
@@ -661,40 +661,43 @@ dumpCreateDB(PGconn *conn)
661661
char *dbtablespace = PQgetvalue(res, i, 5);
662662
char *fdbname;
663663

664-
if (strcmp(dbname, "template1") == 0)
665-
continue;
666-
667664
buf = createPQExpBuffer();
668-
669-
/* needed for buildACLCommands() */
670665
fdbname = strdup(fmtId(dbname));
671666

672-
if (output_clean)
673-
appendPQExpBuffer(buf, "DROP DATABASE %s;\n", fdbname);
674-
675-
appendPQExpBuffer(buf, "CREATE DATABASE %s", fdbname);
667+
/*
668+
* Skip the CREATE DATABASE commands for "template1" and "postgres",
669+
* since they are presumably already there in the destination cluster.
670+
* We do want to emit their ACLs and config options if any, however.
671+
*/
672+
if (strcmp(dbname, "template1") != 0 &&
673+
strcmp(dbname, "postgres") != 0)
674+
{
675+
if (output_clean)
676+
appendPQExpBuffer(buf, "DROP DATABASE %s;\n", fdbname);
676677

677-
appendPQExpBuffer(buf, " WITH TEMPLATE = template0");
678+
appendPQExpBuffer(buf, "CREATE DATABASE %s", fdbname);
678679

679-
if (strlen(dbowner) != 0)
680-
appendPQExpBuffer(buf, " OWNER = %s",
681-
fmtId(dbowner));
680+
appendPQExpBuffer(buf, " WITH TEMPLATE = template0");
682681

683-
appendPQExpBuffer(buf, " ENCODING = ");
684-
appendStringLiteral(buf, dbencoding, true);
682+
if (strlen(dbowner) != 0)
683+
appendPQExpBuffer(buf, " OWNER = %s", fmtId(dbowner));
685684

686-
/* Output tablespace if it isn't default */
687-
if (strcmp(dbtablespace, "pg_default") != 0)
688-
appendPQExpBuffer(buf, " TABLESPACE = %s",
689-
fmtId(dbtablespace));
685+
appendPQExpBuffer(buf, " ENCODING = ");
686+
appendStringLiteral(buf, dbencoding, true);
690687

691-
appendPQExpBuffer(buf, ";\n");
688+
/* Output tablespace if it isn't default */
689+
if (strcmp(dbtablespace, "pg_default") != 0)
690+
appendPQExpBuffer(buf, " TABLESPACE = %s",
691+
fmtId(dbtablespace));
692692

693-
if (strcmp(dbistemplate, "t") == 0)
694-
{
695-
appendPQExpBuffer(buf, "UPDATE pg_database SET datistemplate = 't' WHERE datname = ");
696-
appendStringLiteral(buf, dbname, true);
697693
appendPQExpBuffer(buf, ";\n");
694+
695+
if (strcmp(dbistemplate, "t") == 0)
696+
{
697+
appendPQExpBuffer(buf, "UPDATE pg_database SET datistemplate = 't' WHERE datname = ");
698+
appendStringLiteral(buf, dbname, true);
699+
appendPQExpBuffer(buf, ";\n");
700+
}
698701
}
699702

700703
if (!skip_acls &&
@@ -708,11 +711,12 @@ dumpCreateDB(PGconn *conn)
708711
}
709712

710713
printf("%s", buf->data);
711-
destroyPQExpBuffer(buf);
712-
free(fdbname);
713714

714715
if (server_version >= 70300)
715716
dumpDatabaseConfig(conn, dbname);
717+
718+
destroyPQExpBuffer(buf);
719+
free(fdbname);
716720
}
717721

718722
PQclear(res);

0 commit comments

Comments
 (0)