Skip to content

Commit d1cffe2

Browse files
committed
Add --encoding to pg_dump.
The Problem: Occassionally a DBA needs to dump a database to a new encoding. In instances where the current encoding, (or lack of an encoding, like SQL_ASCII) is poorly supported on the target database server, it can be useful to dump into a particular encoding. But, currently the only way to set the encoding of a pg_dump file is to change client_encoding in postgresql.conf and restart postmaster. This is more than a little awkward for production systems. Magnus Hagander
1 parent 0793108 commit d1cffe2

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

doc/src/sgml/ref/pg_dump.sgml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/pg_dump.sgml,v 1.78 2005/06/21 20:45:43 tgl Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/pg_dump.sgml,v 1.79 2005/07/10 15:08:52 momjian Exp $
33
PostgreSQL documentation
44
-->
55

@@ -194,6 +194,18 @@ PostgreSQL documentation
194194
</listitem>
195195
</varlistentry>
196196

197+
<varlistentry>
198+
<term><option>-E <replaceable class="parameter">encoding</replaceable></option></term>
199+
<term><option>--encoding=<replaceable class="parameter">encoding</replaceable></option></term>
200+
<listitem>
201+
<para>
202+
Create the dump in the specified encoding. By default, the dump is
203+
created in the database encoding.
204+
</para>
205+
</listitem>
206+
</varlistentry>
207+
208+
197209
<varlistentry>
198210
<term><option>-f <replaceable class="parameter">file</replaceable></option></term>
199211
<term><option>--file=<replaceable class="parameter">file</replaceable></option></term>

src/bin/pg_dump/pg_dump.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* by PostgreSQL
1313
*
1414
* IDENTIFICATION
15-
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.414 2005/07/10 14:26:29 momjian Exp $
15+
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.415 2005/07/10 15:08:52 momjian Exp $
1616
*
1717
*-------------------------------------------------------------------------
1818
*/
@@ -185,6 +185,7 @@ main(int argc, char **argv)
185185
const char *pghost = NULL;
186186
const char *pgport = NULL;
187187
const char *username = NULL;
188+
const char *dumpencoding = NULL;
188189
bool oids = false;
189190
TableInfo *tblinfo;
190191
int numTables;
@@ -231,6 +232,7 @@ main(int argc, char **argv)
231232
{"no-privileges", no_argument, NULL, 'x'},
232233
{"no-acl", no_argument, NULL, 'x'},
233234
{"compress", required_argument, NULL, 'Z'},
235+
{"encoding", required_argument, NULL, 'E'},
234236
{"help", no_argument, NULL, '?'},
235237
{"version", no_argument, NULL, 'V'},
236238

@@ -276,7 +278,7 @@ main(int argc, char **argv)
276278
}
277279
}
278280

279-
while ((c = getopt_long(argc, argv, "abcCdDf:F:h:in:oOp:RsS:t:uU:vWxX:Z:",
281+
while ((c = getopt_long(argc, argv, "abcCdDE:f:F:h:in:oOp:RsS:t:uU:vWxX:Z:",
280282
long_options, &optindex)) != -1)
281283
{
282284
switch (c)
@@ -308,6 +310,10 @@ main(int argc, char **argv)
308310
attrNames = true;
309311
break;
310312

313+
case 'E': /* Dump encoding */
314+
dumpencoding = optarg;
315+
break;
316+
311317
case 'f':
312318
filename = optarg;
313319
break;
@@ -515,6 +521,15 @@ main(int argc, char **argv)
515521
/* Set the datestyle to ISO to ensure the dump's portability */
516522
do_sql_command(g_conn, "SET DATESTYLE = ISO");
517523

524+
/* Set the client encoding */
525+
if (dumpencoding)
526+
{
527+
char *cmd = malloc(strlen(dumpencoding) + 32);
528+
sprintf(cmd,"SET client_encoding='%s'", dumpencoding);
529+
do_sql_command(g_conn, cmd);
530+
free(cmd);
531+
}
532+
518533
/*
519534
* If supported, set extra_float_digits so that we can dump float data
520535
* exactly (given correctly implemented float I/O code, anyway)
@@ -662,6 +677,7 @@ help(const char *progname)
662677
printf(_(" -C, --create include commands to create database in dump\n"));
663678
printf(_(" -d, --inserts dump data as INSERT, rather than COPY, commands\n"));
664679
printf(_(" -D, --column-inserts dump data as INSERT commands with column names\n"));
680+
printf(_(" -E, --encoding=ENCODING dump the data in encoding ENCODING\n"));
665681
printf(_(" -n, --schema=SCHEMA dump the named schema only\n"));
666682
printf(_(" -o, --oids include OIDs in dump\n"));
667683
printf(_(" -O, --no-owner skip restoration of object ownership\n"

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy