postgresql Rails 本机 gem 扩展失败

postgresql Rails native gem extension failure

我正在尝试创建一个新的 rails-api 项目。我从以下命令开始 rails-api new <name> -T -d postgresql,我收到以下命令:

Gem files will remain installed in /var/folders/lk/2ckzjmfx03sgkryx70nc3_c40000gn/T/bundler20151231-18995-t8e1f5pg-0.18.4/gems/pg-0.18.4 for inspection.
Results logged to /var/folders/lk/2ckzjmfx03sgkryx70nc3_c40000gn/T/bundler20151231-18995-t8e1f5pg-0.18.4/gems/pg-0.18.4/ext/gem_make.out
An error occurred while installing pg (0.18.4), and Bundler cannot continue.
Make sure that `gem install pg -v '0.18.4'` succeeds before bundling.
         run  bundle exec spring binstub --all
/Library/Ruby/Gems/2.0.0/gems/bundler-1.10.6/lib/bundler/resolver.rb:347:in `block in verify_gemfile_dependencies_are_found!': Could not find gem 'pg (>= 0) ruby' in any of the gem sources listed in your Gemfile or available on this machine. (Bundler::GemNotFound)

我跟进 sudo gem install pg -v '0.18.0',我收到以下错误:

Building native extensions.  This could take a while...
ERROR:  Error installing pg:
    ERROR: Failed to build gem native extension.

    /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby extconf.rb
checking for pg_config... yes
Using config values from /usr/local/bin/pg_config
checking for libpq-fe.h... yes
checking for libpq/libpq-fs.h... yes
checking for pg_config_manual.h... yes
checking for PQconnectdb() in -lpq... no
checking for PQconnectdb() in -llibpq... no
checking for PQconnectdb() in -lms/libpq... no
Can't find the PostgreSQL client library (libpq)
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

此时我不确定我能做什么。我可以找到/usr/local/bin/pg_config的路径 只是不知道如何解决这个问题。任何帮助将不胜感激。

我也尝试了 ARCHFLAGS="-arch i386" bundle install(相同的结果)和 ARCHFLAGS="-arch x86_64" bundle install 并得到:

Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

    /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby extconf.rb --with-pg-config=/usr/pgsql-9.2/bin/pg_config
Using config values from /usr/pgsql-9.2/bin/pg_config
sh: /usr/pgsql-9.2/bin/pg_config: No such file or directory
sh: /usr/pgsql-9.2/bin/pg_config: No such file or directory
checking for libpq-fe.h... yes
checking for libpq/libpq-fs.h... yes
checking for pg_config_manual.h... yes
checking for PQconnectdb() in -lpq... yes
checking for PQconnectionUsedPassword()... yes
checking for PQisthreadsafe()... yes
checking for PQprepare()... yes
checking for PQexecParams()... yes
checking for PQescapeString()... yes
checking for PQescapeStringConn()... yes
checking for PQescapeLiteral()... yes
checking for PQescapeIdentifier()... yes
checking for PQgetCancel()... yes
checking for lo_create()... yes
checking for pg_encoding_to_char()... yes
checking for pg_char_to_encoding()... yes
checking for PQsetClientEncoding()... yes
checking for PQlibVersion()... yes
checking for PQping()... yes
checking for PQsetSingleRowMode()... yes
checking for PQconninfo()... yes
checking for rb_encdb_alias()... yes
checking for rb_enc_alias()... yes
checking for rb_thread_call_without_gvl()... yes
checking for rb_thread_call_with_gvl()... yes
checking for rb_thread_fd_select()... yes
checking for rb_w32_wrap_io_handle()... no
checking for rb_str_modify_expand()... yes
checking for rb_hash_dup()... yes
checking for PGRES_COPY_BOTH in libpq-fe.h... yes
checking for PGRES_SINGLE_TUPLE in libpq-fe.h... yes
checking for PG_DIAG_TABLE_NAME in libpq-fe.h... yes
checking for struct pgNotify.extra in libpq-fe.h... yes
checking for unistd.h... yes
checking for inttypes.h... yes
checking for ruby/st.h... yes
checking for C99 variable length arrays... yes
creating extconf.h
creating Makefile

make "DESTDIR="
compiling gvl_wrappers.c
compiling pg.c
compiling pg_binary_decoder.c
pg_binary_decoder.c:52:1: warning: control may reach end of non-void function [-Wreturn-type]
}
^
pg_binary_decoder.c:83:1: warning: control may reach end of non-void function [-Wreturn-type]
}
^
2 warnings generated.
compiling pg_binary_encoder.c
compiling pg_coder.c
pg_coder.c:24:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
1 warning generated.
compiling pg_connection.c
compiling pg_copy_coder.c
compiling pg_errors.c
compiling pg_result.c
compiling pg_text_decoder.c
compiling pg_text_encoder.c
compiling pg_type_map.c
compiling pg_type_map_all_strings.c
compiling pg_type_map_by_class.c
compiling pg_type_map_by_column.c
compiling pg_type_map_by_mri_type.c
compiling pg_type_map_by_oid.c
compiling pg_type_map_in_ruby.c
pg_type_map_in_ruby.c:145:1: warning: control may reach end of non-void function [-Wreturn-type]
}
^
1 warning generated.
compiling util.c
linking shared-object pg_ext.bundle
ld: file not found: dynamic_lookup
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [pg_ext.bundle] Error 1

再次感谢您花时间帮助我。

编译本机扩展需要通过系统安装程序安装一些开发库。

Can't find the PostgreSQL client library (libpq)

库是在您安装 postgresql 时由 homebrew 安装的。但是您没有指定正确的体系结构,这就是找不到它的原因。指定 x86_64 后找到库:

checking for PQconnectdb() in -lpq... yes

但是他的时间错误pg_config被使用了:

Using config values from /usr/pgsql-9.2/bin/pg_config
sh: /usr/pgsql-9.2/bin/pg_config: No such file or directory
sh: /usr/pgsql-9.2/bin/pg_config: No such file or directory

所以需要手动指定,像这样:

ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=/usr/local/bin/pg_config