Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

macos - Ruby 'pg' gem linking to wrong copy of libpq.5.dylib (on OSX)

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.)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...