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 = '*'
解决步骤
修正odbc.ini配置错误
错误信息显示ODBC解析主机名时包含了注释内容,说明odbc.ini中Servername行的格式有误。将Servername=localhost(含末尾空格)修改为:Servername=localhost注意:ODBC配置文件中,行尾注释需单独换行,同一行的注释会被解析为配置值的一部分。
验证ODBC驱动路径
确认odbcinst.ini中指定的驱动文件存在:ls /usr/local/lib/psqlodbcw.so若文件不存在,通过brew重新安装ODBC驱动:
brew install psqlodbc并将驱动路径更新为实际安装路径(可通过
find /usr/local -name psqlodbc*.so查找)。重启PostgreSQL服务
重启服务确保配置生效:brew services restart postgresql重新测试连接
执行命令再次验证ODBC连接:isql -v PostgresDSN
内容的提问来源于stack exchange,提问作者Harish
相关产品推荐
相关产品推荐

