You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

tds_fdw/FreeTDS字符集异常:强制CP1252引发转UTF-8报错求助

解决tds_fdw连接Azure MSSQL与PostgreSQL时的字符集转换问题

问题概述

使用tds_fdw连接Azure MSSQL与PostgreSQL时,出现字符集转换报错:FreeTDS自动将服务器端单字节字符集设为CP1252,因数据中包含\x81字符导致转换失败。目前未明确FreeTDS选择CP1252的原因,需找到规避方法。

环境信息

  • Azure MSSQL:所有字符串列编码为SQL_Latin1_General_CP1_CI_AS
  • PostgreSQL 15:服务器字符集为UTF-8
  • 连接组件:tds_fdw 7.4

错误场景

连接可正常建立,多数查询能返回结果,但查询某包含特定内容的列时触发错误:

  • 目标列内容:Å TEPÃ�N(对应字节序列:'\xc5\xa0\x54\x45\x50\xc3\x81\x4e')
  • 报错信息:
SQL Error [HV00L]: ERROR: DB-Library error: DB #: 2403, DB Msg: Some character(s) could not be converted into client's character set.  Unconverted bytes were changed to question marks ('?'), OS #: 0, OS Msg: Succès, Level: 4
  • FreeTDS日志显示的字符集设置:
(iconv.c:827):setting server single-byte charset to "CP1252"

关键测试验证

  • 使用ISO-8859-1转UTF-8可正常完成转换:
    echo -e -n '\xc5\xa0\x54\x45\x50\xc3\x81\x4e' | iconv --from iso8859-1 --to utf8
    
  • 使用CP1252转UTF-8则触发转换错误:
    echo -e -n '\xc5\xa0\x54\x45\x50\xc3\x81\x4e' | iconv --from CP1252 --to utf8
    
    执行结果:
    ŠTEP�iconv: séquence d'échappement non permise à la position 6
    
  • 补充:将MSSQL数据导出为CSV后再导入PostgreSQL,无字符转换报错。

需求

寻求配置tds_fdw或FreeTDS的方法,使其使用ISO_8859_1而非CP1252作为单字节字符集。


内容的提问来源于stack exchange,提问作者vmx24

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 20:35:14