4

我使用 UTF8 作为我的 Postgres 8.4.11 数据库的编码:

CREATE DATABASE test
  WITH OWNER = postgres
       ENCODING = 'UTF8'
       TABLESPACE = mydata
       LC_COLLATE = 'de_DE.UTF-8'
       LC_CTYPE = 'de_DE.UTF-8'
       CONNECTION LIMIT = -1;

ALTER DATABASE test SET default_tablespace='mydata';
ALTER DATABASE test SET temp_tablespaces=mydata;

和输出\l

 test | postgres  | UTF8     | de_DE.UTF-8 | de_DE.UTF-8 |

当我尝试插入德语字符时:

create table x(a text);

insert into x values('ä,ß,ö');
ERROR:  invalid byte sequence for encoding "UTF8": 0xe42cdf
HINT:  This error can also happen if the byte sequence does not match the encoding expected by the server, which is controlled by "client_encoding".

我正在使用腻子连接。任何想法?

4

1 回答 1

8

关键元素是client_encoding- 服务器期望您的客户端提供的编码。它必须与实际发送的内容相匹配。你得到show client_encoding什么?是UNICODE吗?

在手册的服务器和客户端之间的自动字符集转换一章中阅读更多内容。

如果您使用psql作为客户端,您可以client_encoding使用\encoding. 检查本地系统用户的编码(在 shell 中的 Linux 类型上)并在 psql 中locale设置匹配。client_encoding如果您在系统上使用与encodingPostgreSQL 服务器相同的语言环境,则可以避免这种复杂情况。

如果您使用puTTY(在 Windows 上),请确保相应地设置其“翻译”。看看设置:窗口 - 翻译。必须匹配client_encoding。您可以右键单击正在运行的会话并选择Change Settings。您还可以将这些设置与您保存的会话一起保存。

于 2012-07-11T10:45:30.087 回答