Skip to content

Commit 84be671

Browse files
committed
pg_dumpall: Add a -E flag to set the encoding, like pg_dump has.
Michael Paquier, reviewed by Fabien Coelho Discussion: http://postgr.es/m/CAB7nPqQcYWmrm2n-dVaMUhYPKFU_DxQwPuUGuC4ZF+8B=dS5xQ@mail.gmail.com
1 parent 2f5ada2 commit 84be671

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

doc/src/sgml/ref/pg_dumpall.sgml

+13
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,19 @@ PostgreSQL documentation
9999
</listitem>
100100
</varlistentry>
101101

102+
<varlistentry>
103+
<term><option>-E <replaceable class="parameter">encoding</replaceable></option></term>
104+
<term><option>--encoding=<replaceable class="parameter">encoding</replaceable></option></term>
105+
<listitem>
106+
<para>
107+
Create the dump in the specified character set encoding. By default,
108+
the dump is created in the database encoding. (Another way to get the
109+
same result is to set the <envar>PGCLIENTENCODING</envar> environment
110+
variable to the desired dump encoding.)
111+
</para>
112+
</listitem>
113+
</varlistentry>
114+
102115
<varlistentry>
103116
<term><option>-f <replaceable class="parameter">filename</replaceable></option></term>
104117
<term><option>--file=<replaceable class="parameter">filename</replaceable></option></term>

src/bin/pg_dump/pg_dumpall.c

+23-1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ main(int argc, char *argv[])
9797
static struct option long_options[] = {
9898
{"data-only", no_argument, NULL, 'a'},
9999
{"clean", no_argument, NULL, 'c'},
100+
{"encoding", required_argument, NULL, 'E'},
100101
{"file", required_argument, NULL, 'f'},
101102
{"globals-only", no_argument, NULL, 'g'},
102103
{"host", required_argument, NULL, 'h'},
@@ -147,6 +148,7 @@ main(int argc, char *argv[])
147148
char *pguser = NULL;
148149
char *pgdb = NULL;
149150
char *use_role = NULL;
151+
const char *dumpencoding = NULL;
150152
trivalue prompt_password = TRI_DEFAULT;
151153
bool data_only = false;
152154
bool globals_only = false;
@@ -204,7 +206,7 @@ main(int argc, char *argv[])
204206

205207
pgdumpopts = createPQExpBuffer();
206208

207-
while ((c = getopt_long(argc, argv, "acd:f:gh:l:oOp:rsS:tU:vwWx", long_options, &optindex)) != -1)
209+
while ((c = getopt_long(argc, argv, "acd:E:f:gh:l:oOp:rsS:tU:vwWx", long_options, &optindex)) != -1)
208210
{
209211
switch (c)
210212
{
@@ -221,6 +223,12 @@ main(int argc, char *argv[])
221223
connstr = pg_strdup(optarg);
222224
break;
223225

226+
case 'E':
227+
dumpencoding = pg_strdup(optarg);
228+
appendPQExpBufferStr(pgdumpopts, " -E ");
229+
appendShellString(pgdumpopts, optarg);
230+
break;
231+
224232
case 'f':
225233
filename = pg_strdup(optarg);
226234
appendPQExpBufferStr(pgdumpopts, " -f ");
@@ -453,6 +461,19 @@ main(int argc, char *argv[])
453461
else
454462
OPF = stdout;
455463

464+
/*
465+
* Set the client encoding if requested.
466+
*/
467+
if (dumpencoding)
468+
{
469+
if (PQsetClientEncoding(conn, dumpencoding) < 0)
470+
{
471+
fprintf(stderr, _("%s: invalid client encoding \"%s\" specified\n"),
472+
progname, dumpencoding);
473+
exit_nicely(1);
474+
}
475+
}
476+
456477
/*
457478
* Get the active encoding and the standard_conforming_strings setting, so
458479
* we know how to escape strings.
@@ -588,6 +609,7 @@ help(void)
588609
printf(_("\nOptions controlling the output content:\n"));
589610
printf(_(" -a, --data-only dump only the data, not the schema\n"));
590611
printf(_(" -c, --clean clean (drop) databases before recreating\n"));
612+
printf(_(" -E, --encoding=ENCODING dump the data in encoding ENCODING\n"));
591613
printf(_(" -g, --globals-only dump only global objects, no databases\n"));
592614
printf(_(" -o, --oids include OIDs in dump\n"));
593615
printf(_(" -O, --no-owner skip restoration of object ownership\n"));

0 commit comments

Comments
 (0)