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

Mac下brew安装PostgreSQL后ODBC连接失败求助

Mac下ODBC连接PostgreSQL失败排查与解决

问题背景

在Mac上通过ODBC连接PostgreSQL时遇到错误,尝试使用localhost、127.0.0.1及本机IPv4地址均无法解决,但通过psql -U postgres可正常连接数据库,PostgreSQL为brew默认安装。

执行命令及错误信息

isql -v PostgresDSN

[08001][unixODBC]could not translate host name "localhost  # Replace with your server's hostname or IP" to address: nodename nor servname provided, or not known

[ISQL]ERROR: Could not SQLConnect

相关配置文件内容

/usr/local/etc/odbc.ini

[PostgresDSN]
Description=PostgreSQL Data Source
Driver=PostgreSQL
Servername=localhost  
Port=5432            
Database=test        
Username=test1      
Password=password 

/usr/local/etc/odbcinst.ini

[PostgreSQL]
Description=PostgreSQL ODBC Driver
Driver=/usr/local/lib/psqlodbcw.so 

pg_hba.conf

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     trust
host    replication     all             127.0.0.1/32            trust
host    replication     all             ::1/128                 trust
host    all             all             0.0.0.0/0               trust
host    all             all             ::0/0                   trust

postgresql.conf

listen_addresses = '*'  

解决步骤

  1. 修正odbc.ini配置错误
    错误信息显示ODBC解析主机名时包含了注释内容,说明odbc.ini中Servername行的格式有误。将Servername=localhost (含末尾空格)修改为:

    Servername=localhost
    

    注意:ODBC配置文件中,行尾注释需单独换行,同一行的注释会被解析为配置值的一部分。

  2. 验证ODBC驱动路径
    确认odbcinst.ini中指定的驱动文件存在:

    ls /usr/local/lib/psqlodbcw.so
    

    若文件不存在,通过brew重新安装ODBC驱动:

    brew install psqlodbc
    

    并将驱动路径更新为实际安装路径(可通过find /usr/local -name psqlodbc*.so查找)。

  3. 重启PostgreSQL服务
    重启服务确保配置生效:

    brew services restart postgresql
    
  4. 重新测试连接
    执行命令再次验证ODBC连接:

    isql -v PostgresDSN
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 21:13:16