MacOS High Sierra下安装pg -v '0.18.4' gem失败求助(RoR)
我之前维护旧Rails项目时,碰到过和你完全一样的状况——在Mac High Sierra上安装pg 0.18.4时,编译器报PQconnectdb类型冲突的错误。结合你的环境(Postgres 9.4、Ruby 2.3.3、Rails 5.1.4),下面是我亲测有效的解决步骤:
核心解决方案:指定正确Postgres配置路径 + 兼容编译器参数
定位Postgres 9.4的
pg_config路径
首先要确保gem链接到你安装的Postgres 9.4,而非系统自带或其他版本的Postgres。可以用find命令查找:find /usr/local -name pg_config -type f你应该能找到类似
/usr/local/Cellar/postgresql/9.4.0.1/bin/pg_config的路径。带参数安装pg gem
别用sudo(避免调用系统Ruby),直接在项目的Ruby环境下执行:gem install pg -v '0.18.4' -- --with-pg-config=/usr/local/Cellar/postgresql/9.4.0.1/bin/pg_config如果还是报错,加上编译器兼容参数,绕过High Sierra clang的严格类型检查:
CFLAGS="-Wno-error=implicit-function-declaration" gem install pg -v '0.18.4' -- --with-pg-config=/usr/local/Cellar/postgresql/9.4.0.1/bin/pg_config确认Ruby环境匹配
如果你用rbenv或rvm,先切换到项目指定的Ruby版本:# rbenv用户 rbenv local 2.3.3p222 # rvm用户 rvm use 2.3.3p222再执行上面的gem安装命令。
错误原因说明
pg 0.18.4是2016年的旧版本,而Mac High Sierra的clang编译器引入了更严格的类型检查。旧版pg gem里对PQconnectdb的声明是extern void PQconnectdb();,但Postgres 9.4中这个函数的实际返回值是PGconn*,编译器因此报类型冲突。
另外,你之前尝试的brew install libpqxx没用,因为libpqxx是Postgres的C++客户端库,而pg gem依赖的是C语言的libpq库,两者没有关联。
内容的提问来源于stack exchange,提问作者Axil

