【问题标题】:PSQL : ERROR: invalid byte sequence for encoding "UTF8"PSQL:错误:编码“UTF8”的字节序列无效
【发布时间】:2017-04-24 11:29:52
【问题描述】:

我正在尝试通过 java PostgreSQL 42.0.0.jre7 API 创建一个逻辑复制槽,这是我的代码:

String url = "jdbc:postgresql://localhost:5432/sampledb?characterEncoding=utf8";
Properties props = new Properties();
PGProperty.USER.set(props, "postgres");
PGProperty.PASSWORD.set(props, "root");
PGProperty.ASSUME_MIN_SERVER_VERSION.set(props, "9.6");
PGProperty.REPLICATION.set(props, "true");
PGProperty.PREFER_QUERY_MODE.set(props, "simple");
DriverManager.registerDriver(new org.postgresql.Driver());
Connection con = DriverManager.getConnection(url, props);
PGConnection replConnection = con.unwrap(PGConnection.class);

replConnection.getReplicationAPI()
.createReplicationSlot()
.logical()
.withSlotName("logical_replication_slot")
.withOutputPlugin("test_decoding")
.make();

但是当我运行它时,我得到这个异常,说我有编码问题。

Caused by: org.postgresql.util.PSQLException: ERROR: invalid byte sequence for encoding "UTF8": 0xe9 0x71 0x75
        at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2412)[423:org.postgresql.jdbc41:42.0.0.jre7]
        at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2125)[423:org.postgresql.jdbc41:42.0.0.jre7]
        at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:297)[423:org.postgresql.jdbc41:42.0.0.jre7]
        at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:428)[423:org.postgresql.jdbc41:42.0.0.jre7]
        at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:354)[423:org.postgresql.jdbc41:42.0.0.jre7]
        at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:301)[423:org.postgresql.jdbc41:42.0.0.jre7]
        at org.postgresql.jdbc.PgStatement.executeCachedSql(PgStatement.java:287)[423:org.postgresql.jdbc41:42.0.0.jre7]
        at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:264)[423:org.postgresql.jdbc41:42.0.0.jre7]
        at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:260)[423:org.postgresql.jdbc41:42.0.0.jre7]
        at org.postgresql.replication.fluent.logical.LogicalCreateSlotBuilder.make(LogicalCreateSlotBuilder.java:48)[423:org.postgresql.jdbc41:42.0.0.jre7]
        at com.soprahr.hub.replication.postgresql.activator.Activator.start(Activator.java:41)[424:com.soprahr.hub.replication.postgresql:0.0.1.SNAPSHOT]
        at org.apache.felix.framework.util.SecureAction.startActivator(SecureAction.java:697)[org.apache.felix.framework-5.6.1.jar:]
        at org.apache.felix.framework.Felix.activateBundle(Felix.java:2226)[org.apache.felix.framework-5.6.1.jar:]
        ... 16 more

这是我的 postgres 配置,我尝试使用 sql_ascii、utf8 进行客户端编码,但总是出现相同的错误:

client_encoding = windows-1251      # actually, defaults to database
                    # encoding

# These settings are initialized by initdb, but they can be changed.
lc_messages = 'French_France.1252'          # locale for system error message
                    # strings
lc_monetary = 'French_France.1252'          # locale for monetary formatting
lc_numeric = 'French_France.1252'           # locale for number formatting
lc_time = 'French_France.1252'              # locale for time formatting

【问题讨论】:

  • 也许您应该将 client_encoding 设置为 cp1252、iso8859-1 或 iso8859-15 ? (您也可以在会话开始时通过set client_encoding iso8859-15; 进行设置
  • 我无法设置它显示以下错误未连接到服务器或与服务器的连接已关闭。但是当我try show client_encoding 它给了 UNICODE 我试图从配置文件中编辑它但总是 UNICODE

标签: java postgresql


【解决方案1】:

您只需将设置更改为 postgres.conf

lc_messages = 'English_United States.1252' 
lc_monetary = 'English_United States.1252'  
lc_numeric = 'English_United States.1252'   
lc_time = 'English_United States.1252'      
default_text_search_config = 'pg_catalog.english'

别忘了评论“client_encoding = windows-1251”。

这应该可行!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-01
    • 2022-01-26
    • 2011-06-19
    • 1970-01-01
    相关资源
    最近更新 更多