How do I change the default client_encoding in Postgres?
<p>I'm trying to change the default value for the <code>client_encoding</code> configuration variable for a PostgreSQL database I'm running. I want it to be <code>UTF8</code>, but currently it's getting set to <code>LATIN1</code>.</p>

<p>The database is already set to use UTF8 encoding:</p>

<pre class="lang-none prettyprint-override"><code>application_database=# \l
                                                 List of databases
           Name       |  Owner   | Encoding |   Collate   |    Ctype    |          Access privileges
----------------------+----------+----------+-------------+-------------+--------------------------------------
 postgres             | postgres | LATIN1   | en_US       | en_US       |
 application_database | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | postgres=CTc/postgres           +
                      |          |          |             |             | application_database=Tc/postgres
 template0            | postgres | LATIN1   | en_US       | en_US       | =c/postgres                     +
                      |          |          |             |             | postgres=CTc/postgres
 template1            | postgres | LATIN1   | en_US       | en_US       | =c/postgres                     +
                      |          |          |             |             | postgres=CTc/postgres
(4 rows)
</code></pre>

<p>Which <a href="http://www.postgresql.org/docs/9.1/static/runtime-config-client.html#GUC-CLIENT-ENCODING" rel="noreferrer">according to the docs</a> should <em>already</em> result in the client using UTF8 as its default <code>client_encoding</code> (emphasis mine):</p>

<blockquote>
  <p><code>client_encoding (string)</code></p>
  
  <p>Sets the client-side encoding (character set). <strong>The default is to use the database encoding.</strong></p>
</blockquote>

<p>But it doesn't:</p>

<pre class="lang-none prettyprint-override"><code>$ sudo psql --dbname=application_database
psql (9.1.19)
Type "help" for help.

application_database=# SHOW client_encoding;
 client_encoding
-----------------
 LATIN1
(1 row)
</code></pre>

<p>I even tried using <code>ALTER USER &lt;user&gt; SET ...</code> to change the default config for the user I'm logging in as:</p>

<pre class="lang-none prettyprint-override"><code>application_database=# ALTER USER root SET client_encoding='UTF8';
ALTER ROLE
application_database=# SELECT usename, useconfig FROM pg_shadow;
         usename      |       useconfig
----------------------+------------------------
 postgres             |
 root                 | {client_encoding=UTF8}
 application_database |
(3 rows)
</code></pre>

<p>But that also had no effect:</p>

<pre class="lang-none prettyprint-override"><code>$ sudo psql --dbname=application_database
psql (9.1.19)
Type "help" for help.

application_database=# SELECT current_user;
 current_user
--------------
 root
(1 row)

application_database=# SHOW client_encoding;
 client_encoding
-----------------
 LATIN1
(1 row)
</code></pre>

<p>There's nothing in any of the PSQL files on my system:</p>

<pre class="lang-none prettyprint-override"><code>vagrant@app-database:~$ cat ~/.psqlrc
cat: /home/vagrant/.psqlrc: No such file or directory
vagrant@app-database:~$ cat /etc/psqlrc
cat: /etc/psqlrc: No such file or directory
vagrant@app-database:~$ sudo su
root@app-database:/home/vagrant# cat ~/.psqlrc
cat: /root/.psqlrc: No such file or directory
</code></pre>

<p>I'm running PosgreSQL 9.1:</p>

<pre class="lang-none prettyprint-override"><code>application_database=# SELECT version();
                                                   version
-------------------------------------------------------------------------------------------------------------
 PostgreSQL 9.1.19 on x86_64-unknown-linux-gnu, compiled by gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3, 64-bit
(1 row)
</code></pre>