>From e29c0c2aa7ec7eca370b877cdafc888d51b4939f Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <heikki.linnakangas@iki.fi>
Date: Mon, 26 Aug 2013 22:00:05 +0300
Subject: [PATCH 2/3] Set client_encoding='auto' in all client utilities.

We've been doing this for psql, when run in a terminal, since 9.1, and it
has worked pretty well, so expand to other utilities. Although the other
utilities don't have an interactive mode, client_encoding also affects the
e.g table names given on the command-line arguments are sent to the server.
---
 doc/src/sgml/ref/pg_dump.sgml  |    3 ++-
 src/bin/pg_dump/pg_backup_db.c |    8 +++++---
 src/bin/scripts/common.c       |    8 +++++---
 3 files changed, 12 insertions(+), 7 deletions(-)

diff --git a/doc/src/sgml/ref/pg_dump.sgml b/doc/src/sgml/ref/pg_dump.sgml
index 007bace..fc7c73f 100644
--- a/doc/src/sgml/ref/pg_dump.sgml
+++ b/doc/src/sgml/ref/pg_dump.sgml
@@ -192,7 +192,8 @@ PostgreSQL documentation
         -E only affects the encoding used for the output. The client encoding
         used to interpret any object names given on the command line is
         determined by the <envar>PGCLIENTENCODING</envar> environment
-        variable.
+        variable, or from the terminal's locale settings if not set
+        (<envar>LC_CTYPE</envar> environment variable on Unix systems).
         </para>
 
         <para>
diff --git a/src/bin/pg_dump/pg_backup_db.c b/src/bin/pg_dump/pg_backup_db.c
index cd2dded..75c82cd 100644
--- a/src/bin/pg_dump/pg_backup_db.c
+++ b/src/bin/pg_dump/pg_backup_db.c
@@ -240,7 +240,7 @@ ConnectDatabase(Archive *AHX,
 	 */
 	do
 	{
-#define PARAMS_ARRAY_SIZE	7
+#define PARAMS_ARRAY_SIZE	8
 		const char **keywords = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*keywords));
 		const char **values = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*values));
 
@@ -256,8 +256,10 @@ ConnectDatabase(Archive *AHX,
 		values[4] = dbname;
 		keywords[5] = "fallback_application_name";
 		values[5] = progname;
-		keywords[6] = NULL;
-		values[6] = NULL;
+		keywords[6] = "client_encoding";
+		values[6] = getenv("PGCLIENTENCODING") ? NULL : "auto";
+		keywords[7] = NULL;
+		values[7] = NULL;
 
 		new_pass = false;
 		AH->connection = PQconnectdbParams(keywords, values, true);
diff --git a/src/bin/scripts/common.c b/src/bin/scripts/common.c
index 4645bc1..bb95b44 100644
--- a/src/bin/scripts/common.c
+++ b/src/bin/scripts/common.c
@@ -107,7 +107,7 @@ connectDatabase(const char *dbname, const char *pghost, const char *pgport,
 	 */
 	do
 	{
-#define PARAMS_ARRAY_SIZE	7
+#define PARAMS_ARRAY_SIZE	8
 		const char **keywords = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*keywords));
 		const char **values = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*values));
 
@@ -123,8 +123,10 @@ connectDatabase(const char *dbname, const char *pghost, const char *pgport,
 		values[4] = dbname;
 		keywords[5] = "fallback_application_name";
 		values[5] = progname;
-		keywords[6] = NULL;
-		values[6] = NULL;
+		keywords[6] = "client_encoding";
+		values[6] = getenv("PGCLIENTENCODING") ? NULL : "auto";
+		keywords[7] = NULL;
+		values[7] = NULL;
 
 		new_pass = false;
 		conn = PQconnectdbParams(keywords, values, true);
-- 
1.7.10.4

