How do I change the default client_encoding in Postgres?

前端 未结 2 897
攒了一身酷
攒了一身酷 2020-12-31 05:26

I\'m trying to change the default value for the client_encoding configuration variable for a PostgreSQL database I\'m running. I want it to be UTF8

相关标签:
2条回答
  • 2020-12-31 05:37

    Okay, so first things first: why isn't setting the user or database encoding having any effect?

    Turns out it's because of this line from the psql documentation:

    If at least one of standard input or standard output are a terminal, then psql sets the client encoding to "auto", which will detect the appropriate client encoding from the locale settings (LC_CTYPE environment variable on Unix systems). If this doesn't work out as expected, the client encoding can be overridden using the environment variable PGCLIENTENCODING.

    So, in fact, your previous configuration changes actually have been working, just not in the interactive psql console. Try the following command:

    sudo psql --dbname=application_database -c "SHOW client_encoding;" | cat
    

    You should see that the client encoding is actually UTF8:

     client_encoding
    -----------------
     UTF8
    (1 row)
    

    Now run the command again, but without piping it to cat:

    sudo psql --dbname=application_database -c "SHOW client_encoding;"
    

    You should get the result:

     client_encoding
    -----------------
     LATIN1
    (1 row)
    

    So basically, psql is only using LATIN1 encoding for commands involving the terminal.

    How can you fix this? Well, there are a few possible ways.

    One would be to do as the docs suggest and set the PGCLIENTENCODING environment variable to UTF8 somewhere persistent, such as ~/.profile or ~/.bashrc to affect only your user, or /etc/environment to affect the whole system (see How to permanently set environmental variables).

    Another option would be to configure your system locale settings to use en_US.utf8 instead of en_US (or equivalent). The method for doing this may vary depending on your system, but usually you can do it by modifying ~/.config/locale.conf (your user only) or /etc/default/locale or /etc/locale.conf (system-wide). This will affect more than just postgres, and I believe more closely addresses the root of the problem. You can check your current locale settings by running locale.

    One other solution would be to update your psqlrc file to include SET client_encoding=UTF8;. That file is located at ~/.psqlrc (your user only) or /etc/psqlrc (system-wide). Note that this method won't affect the result of the command we were using to the client encoding earlier, since the docs state (emphasis mine):

    --command=command

    Specifies that psql is to execute one command string, command, and then exit. This is useful in shell scripts. Start-up files (psqlrc and ~/.psqlrc) are ignored with this option.

    0 讨论(0)
  • 2020-12-31 05:45

    Did you set client_encoding in postgresql.conf (and reload config or restart)? Make sure it's UTF8 not utf8

    What is the result of cat ~/.psqlrc and cat /etc/psqlrc ?

    I know you're looking for server-side default, but on the client, you can set an OS envvar:

    export PGCLIENTENCODING=UTF8

    to do this for all users (on that machine), put that in /etc/profile

    0 讨论(0)
提交回复
热议问题