prepare_conn(ProxyFunction *func, ProxyConnection *conn)
 {
        struct timeval now;
+       StringInfoData connstr;
 
        gettimeofday(&now, NULL);
 
 
        conn->connect_time = now.tv_sec;
 
+       /*
+        * Force client_encoding on server side
+        * to same as current client_encoding.
+        */
+       initStringInfo(&connstr);
+       appendStringInfo(&connstr, "%s client_encoding=%s", conn->connstr,
+                                        pg_get_client_encoding_name());
+
        /* launch new connection */
        conn->db = PQconnectStart(conn->connstr);
        if (conn->db == NULL)
 
        if (PQstatus(conn->db) == CONNECTION_BAD)
                conn_error(func, conn, "PQconnectStart");
+
+       /* free connstr early */
+       pfree(connstr.data);
 }
 
 /*