8000 Fix pg_dumpall with database names containing = · danielcode/postgres@79f21b3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 79f21b3

Browse files
committed
Fix pg_dumpall with database names containing =
If a database name contained a '=' character, pg_dumpall failed. The problem was in the way pg_dumpall passes the database name to pg_dump on the command line. If it contained a '=' character, pg_dump would interpret it as a libpq connection string instead of a plain database name. To fix, pass the database name to pg_dump as a connection string, "dbname=foo", with the database name escaped if necessary. Back-patch to all supported branches.
1 parent 5c97528 commit 79f21b3

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

src/bin/pg_dump/pg_dumpall.c

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ static void makeAlterConfigCommand(PGconn *conn, const char *arrayitem,
4949
static void dumpDatabases(PGconn *conn);
5050
static void dumpTimestamp(char *msg);
5151
static void doShellQuoting(PQExpBuffer buf, const char *str);
52+
static void doConnStrQuoting(PQExpBuffer buf, const char *str);
5253

5354
static int runPgDump(const char *dbname);
5455
static void buildShSecLabels(PGconn *conn, const char *catalog_name,
@@ -1619,6 +1620,7 @@ dumpDatabases(PGconn *conn)
16191620
static int
16201621
runPgDump(const char *dbname)
16211622
{
1623+
PQExpBuffer connstr = createPQExpBuffer();
16221624
PQExpBuffer cmd = createPQExpBuffer();
16231625
int ret;
16241626

@@ -1634,7 +1636,17 @@ runPgDump(const char *dbname)
16341636
else
16351637
appendPQExpBuffer(cmd, " -Fp ");
16361638

1637-
doShellQuoting(cmd, dbname);
1639+
/*
1640+
* Construct a connection string from the database name, like
1641+
* dbname='<database name>'. pg_dump would usually also accept the
1642+
* database name as is, but if it contains any = characters, it would
1643+
* incorrectly treat it as a connection string.
1644+
*/
1645+
appendPQExpBuffer(connstr, "dbname='");
1646+
doConnStrQuoting(connstr, dbname);
1647+
appendPQExpBuffer(connstr, "'");
1648+
1649+
doShellQuoting(cmd, connstr->data);
16381650

16391651
appendPQExpBuffer(cmd, "%s", SYSTEMQUOTE);
16401652

@@ -1647,6 +1659,7 @@ runPgDump(const char *dbname)
16471659
ret = system(cmd->data);
16481660

16491661
destroyPQExpBuffer(cmd);
1662+
destroyPQExpBuffer(connstr);
16501663

16511664
return ret;
16521665
}
@@ -1886,6 +1899,25 @@ dumpTimestamp(char *msg)
18861899
}
18871900

18881901

1902+
/*
1903+
* Append the given string to the buffer, with suitable quoting for passing
1904+
* the string as a value, in a keyword/pair value in a libpq connection
1905+
* string
1906+
*/
1907+
static void
1908+
doConnStrQuoting(PQExpBuffer buf, const char *str)
1909+
{
1910+
while (*str)
1911+
{
1912+
/* ' and \ must be escaped by to \' and \\ */
1913+
if (*str == '\'' || *str == '\\')
1914+
appendPQExpBufferChar(buf, '\\');
1915+
1916+
appendPQExpBufferChar(buf, *str);
19 5AFC 17+
str++;
1918+
}
1919+
}
1920+
18891921
/*
18901922
* Append the given string to the shell command being built in the buffer,
18911923
* with suitable shell-style quoting.

0 commit comments

Comments
 (0)
0