问题
The EnterpriseDB installer for PostgreSQL 9.3 places its files in /Library/PostgreSQL/9.3/*
on Mac OSX. However, the Ruby gem 'pg' loads a legacy version of the 'C' dynamic library libpq.5.dylib
from /usr/lib
instead of using the correct version from /Library/PostgreSQL/9.3/lib
. This occurs despite my having installed the gem with
gem install pg -- --with-pg-config=/Library/PostgreSQL/9.3/bin/pg_config
I determined which version of libpq.5.dylib
is loaded by setting the DYLD_PRINT_LIBRARIES environment variable, prior to requiring the 'pg' gem.
The most obvious consequence of linking to the wrong dynamic library is that the gem fails to connect to the database using a domain socket, because the socket was relocated between versions from /var/pgsql_socket
to /tmp
. However, linking to the old library may also cause other issues.
Any suggestions on how to fix this problem?
(I am running Mac OSX Lion. Additional note: For most pg libraries, the EnterpriseDB installer put both a static (.a) and a dynamic (.dylib) version in /Library/PostgreSQL/9.3/lib
, but for libpq.5
installed only a dynamic version.)
回答1:
Both the cause of the problem and an easy solution became apparent when I ran bin/pg_config
at the command line.
pg_config
generates the variables that are used to control compilation and linking. Of particular interest are LIBDIR and LDFLAGS. LIBDIR specifies the location for static libraries, while LDFLAGS provides locations to search for dynamic libraries. On my system, LIBDIR was set correctly to /LibraryPostgreSQL/9.3/lib
, but LDFLAGS was set as follows:
LDFLAGS = -L../../../src/common -L/usr/local/lib -L/opt/local/20140109/lib -Wl,-dead-strip-dylibs
Since libpq.5.dylib
was not present at any of these locations, the gem failed to find it, and instead found an older version that happened to be installed at /usr/lib
.
One way to fix this would be to inject the correct file location into LDFLAGS, possibly by modifying the code in extconf.rb
that generates the config file. However, a much easier fix in this case is just to add a symlink in /usr/local/lib
to the correct file location:
/usr/local/lib> ln -s /Library/PostgreSQL/9.3/lib/libpq.5.dylib libpq.5.dylib
If you run into a similar issue, just examine the output of pg_config
, and see if you can place a symlink to the correct file location in one of the directories that is already specified by LDFLAGS.
来源:https://stackoverflow.com/questions/24627465/ruby-pg-gem-linking-to-wrong-copy-of-libpq-5-dylib-on-osx