Error requiring pg under rvm with postgres.app

2019-02-22 15:26发布

问题:

I'm using Postgres.app on OS X (10.8.3). I have modified my PATH so that the bin folder for the app is before all others.

Rammy:~ phrogz$ which pg_config
/Applications/Postgres.app/Contents/MacOS/bin/pg_config

I have rvm installed and can install the pg gem without error, but when I go to require it I get an error:

Rammy:~ phrogz$ gem -v
1.8.25

Rammy:~ phrogz$ gem install pg
Fetching: pg-0.15.1.gem (100%)
Building native extensions.  This could take a while...
Successfully installed pg-0.15.1
1 gem installed

Rammy:~ phrogz$ ruby -v -e "require 'pg'"
ruby 1.9.3p392 (2013-02-22 revision 39386) [x86_64-darwin12.3.0]
/Users/phrogz/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': dlopen(/Users/phrogz/.rvm/gems/ruby-1.9.3-p392/gems/pg-0.15.1/lib/pg_ext.bundle, 9): Library not loaded: @executable_path/../lib/libssl.1.0.0.dylib (LoadError)
  Referenced from: /Applications/Postgres.app/Contents/MacOS/lib/libpq.dylib
  Reason: image not found - /Users/phrogz/.rvm/gems/ruby-1.9.3-p392/gems/pg-0.15.1/lib/pg_ext.bundle
    from /Users/phrogz/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from /Users/phrogz/.rvm/gems/ruby-1.9.3-p392/gems/pg-0.15.1/lib/pg.rb:4:in `<top (required)>'
    from /Users/phrogz/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:60:in `require'
    from /Users/phrogz/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:60:in `rescue in require'
    from /Users/phrogz/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:35:in `require'
    from -e:1:in `<main>'

What do I need to do to get the pg gem properly installed?

回答1:

This worked for me:

sudo env ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=/Applications/Postgres93.app/Contents/MacOS/bin/pg_config

Just double check that the /Applications/Postgres93.app.. path exists for you.



回答2:

Edit: Even though this answer currently has more votes than the accepted answer, the accepted answer is far simpler and cleaner.


Remove the Postgres.app binaries from the path when installing the pg gem, and instead use the postgres install built into OS X to configure the gem. The pg library will still correctly connect to the Postgres.app server later on.

Rammy:~ phrogz$ gem uninstall pg
Successfully uninstalled pg-0.15.1

# Modify PATH to remove /Applications/Postgres.app/Contents/MacOS/bin

Rammy:~ phrogz$ gem install pg
Fetching: pg-0.15.1.gem (100%)
Building native extensions.  This could take a while...
Successfully installed pg-0.15.1
1 gem installed

Rammy:~ phrogz$ ruby -v -e "require 'pg'"
ruby 1.9.3p392 (2013-02-22 revision 39386) [x86_64-darwin12.3.0]


回答3:

I found a solution that works for me and requires minimal hacking/configuring. You only need to do this once and it will work for every bundle install. Add the following to your .bash_profile, .bash_rc, or equivalent:

export DYLD_FALLBACK_LIBRARY_PATH=/Applications/Postgres.app/Contents/MacOS/lib:$DYLD_LIBRARY_PATH

(Assuming you installed Postgres.app in the default location). Then restart your terminal session and try again.

Exporting to DYLD_LIBRARY_PATH directly can cause serious problems with other apps that depend on it, but using the fallback path avoids these problems.

See also:

  • https://github.com/PostgresApp/PostgresApp/issues/109#issuecomment-18387546
  • Postgres.app upgrade, now Rails app won't start

EDIT: It seems that setting DYLD_FALLBACK_LIBRARY_PATH causes an error when you try to run psql. To fix this, you can add the following two lines to your .bash_profile:

alias psql="(. ~/.bash_profile; unset DYLD_FALLBACK_LIBRARY_PATH; psql)";

This is assuming that you're using bash and that your .bash_profile is located in your home directory. If that's not the case (or if you're using a .bashrc or other environment setup instead of .bash_profile) change the ~/.bash_profile part of the command to the path to your environment setup script.

The aliased commands basically start a subshell which does not effect your current bash environment. So when it unsets the DYLD_FALLBACK_LIBRARY_PATH variable, it's only temporary. After you exit psql the environment variable will be set again so that rails functions properly.



回答4:

The problem is in the linking of the gem to the Postgres.app. If you link it to the postgres version that ships with osx

Here's a small script:

  • if using rvm with project gemsets: change into your project
  • run the following commands:

    gem uninstall pg
    
    PATH=${PATH/'Postgres.app'/'WRONGFOLDER.app'}
    gem install pg
    PATH=${PATH/'WRONGFOLDER.app'/'Postgres.app'}
    
  • Now everthing should be fine