有什么方法可以永久设置 client_encoding TO 'UTF8' 吗?

Is there any way to SET client_encoding TO 'UTF8' permanent?

我使用以下命令从控制台连接到我的数据库:

psql -U postgres task_db

并做了这个 select 查询:

select * from common.task;

我收到这个错误:

ERROR:  character with byte sequence 0xe5 0xb0 0x8f in encoding "UTF8" has no equivalent in encoding "WIN1252"

并按照此答案中的此命令修复此问题:

SET client_encoding TO 'UTF8';

成功了。

问题:现在我每次从命令行连接到数据库时都必须运行以上命令。有什么方法可以将此编码永久设置吗?

您可以尝试在 .psqlrc file:

中设置此命令

Unless it is passed an -X option, psql attempts to read and execute commands from the system-wide startup file (psqlrc) and then the user's personal startup file (~/.psqlrc), after connecting to the database but before accepting normal commands. These files can be used to set up the client and/or the server to taste, typically with \set and SET commands.

The system-wide startup file is named psqlrc and is sought in the installation's “system configuration” directory, which is most reliably identified by running pg_config --sysconfdir. By default this directory will be ../etc/ relative to the directory containing the PostgreSQL executables. The name of this directory can be set explicitly via the PGSYSCONFDIR environment variable.

The user's personal startup file is named .psqlrc and is sought in the invoking user's home directory. On Windows, which lacks such a concept, the personal startup file is named %APPDATA%\postgresql\psqlrc.conf. The location of the user's startup file can be set explicitly via the PSQLRC environment variable.

Both the system-wide startup file and the user's personal startup file can be made psql-version-specific by appending a dash and the PostgreSQL major or minor release number to the file name, for example ~/.psqlrc-9.2 or ~/.psqlrc-9.2.5. The most specific version-matching file will be read in preference to a non-version-specific file.