Ruby on Rails / PostgreSQL - 启动服务器时出现库未加载错误

8

嘿,如果该问题已在其他主题中得到解答,请提前谅解。我已经在网站上搜索了,但没有找到答案。不过,最接近的相关问题是Ruby on Rails / PostgreSQL - Library not Loaded error when starting server- libq.5.dylib,但它从未得到解决,并且答案对我没有帮助。

我正在尝试在我的Mac OSX 10.6.8上运行PostgreSQL。我已经安装了Ruby 1.9.3p392和Rails 3.2.13。

由于我使用的是10.6.8版本,而PostGresApp需要10.7+,因此我尝试使用手动安装程序(v9.2.3)进行安装。下载链接:http://www.enterprisedb.com/products-services-training/pgdownload

我首先按照默认路径将其安装在/Library/PostgreSQL/9.2/。然后我安装了pg gem(PATH=$PATH:/Library/PostgreSQL/9.2/bin sudo gem install pg)。当我尝试用rails s来启动本地主机时,我遇到了下面的错误。

阅读一些资料后,我猜测可能是路径错误,因此我删除了pg gem,然后使用postgresql卸载程序。然后我在/users/stewartmccoy/Library/PostgreSQL/9.2/中重新安装了pg...然后重新安装了pg gem。但我仍然遇到同样的错误。

您认为我应该如何正确安装PostgreSQL并启动我的rails服务器呢?

stewart-mccoys-macbook:footy_subs stewartmccoy$ rails s
/Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/pg-0.14.1/lib/pg.rb:4:in `require': dlopen(/Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/pg-0.14.1/lib/pg_ext.bundle, 9): Library not loaded: libpq.5.dylib (LoadError)
  Referenced from: /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/pg-0.14.1/lib/pg_ext.bundle
  Reason: image not found - /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/pg-0.14.1/lib/pg_ext.bundle
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/pg-0.14.1/lib/pg.rb:4:in `<top (required)>'
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.4/lib/bundler/runtime.rb:72:in `require'
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.4/lib/bundler/runtime.rb:72:in `block (2 levels) in require'
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.4/lib/bundler/runtime.rb:70:in `each'
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.4/lib/bundler/runtime.rb:70:in `block in require'
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.4/lib/bundler/runtime.rb:59:in `each'
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.4/lib/bundler/runtime.rb:59:in `require'
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.4/lib/bundler.rb:132:in `require'
    from /Users/stewartmccoy/Code/footy_subs/config/application.rb:13:in `<top (required)>'
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/commands.rb:53:in `require'
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/commands.rb:53:in `block in <top (required)>'
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/commands.rb:50:in `tap'
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/commands.rb:50:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'
stewart-mccoys-macbook:footy_subs stewartmccoy$ gem uninstall pgSuccessfully uninstalled pg-0.14.1
stewart-mccoys-macbook:footy_subs stewartmccoy$ PATH=$PATH:/users/stewartmccoy/Library/PostgreSQL/9.2/bin sudo gem install pg
Password:
Fetching: pg-0.14.1.gem (100%)
Building native extensions.  This could take a while...
Successfully installed pg-0.14.1
1 gem installed
Installing ri documentation for pg-0.14.1...
Installing RDoc documentation for pg-0.14.1...
stewart-mccoys-macbook:footy_subs stewartmccoy$ rails s
/Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/pg-0.14.1/lib/pg.rb:4:in `require': dlopen(/Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/pg-0.14.1/lib/pg_ext.bundle, 9): Library not loaded: libpq.5.dylib (LoadError)
  Referenced from: /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/pg-0.14.1/lib/pg_ext.bundle
  Reason: image not found - /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/pg-0.14.1/lib/pg_ext.bundle
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/pg-0.14.1/lib/pg.rb:4:in `<top (required)>'
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.4/lib/bundler/runtime.rb:72:in `require'
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.4/lib/bundler/runtime.rb:72:in `block (2 levels) in require'
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.4/lib/bundler/runtime.rb:70:in `each'
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.4/lib/bundler/runtime.rb:70:in `block in require'
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.4/lib/bundler/runtime.rb:59:in `each'
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.4/lib/bundler/runtime.rb:59:in `require'
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.4/lib/bundler.rb:132:in `require'
    from /Users/stewartmccoy/Code/footy_subs/config/application.rb:13:in `<top (required)>'
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/commands.rb:53:in `require'
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/commands.rb:53:in `block in <top (required)>'
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/commands.rb:50:in `tap'
    from /Users/stewartmccoy/.rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.13/lib/rails/commands.rb:50:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

1
感谢您提供合理的详细信息 - Pg版本,Pg软件包类型,主机操作系统等。 - Craig Ringer
3个回答

9

错误的关键部分是:

Library not loaded: libpq.5.dylib (LoadError)

这表明在运行时Ruby找不到libpq。为了解决这个问题,您可能需要设置DYLD_LIBRARY_PATH环境变量,指向您的PostgreSQL安装的lib目录,在全局范围内或在用于启动Rails的包装脚本中。有关更多信息,请参见此superuser问题Pg gem可以在编译和安装期间查找库,因为pg_config可执行文件在PATH上,并使用它来查找libpq。它似乎不会存储路径以供运行时使用,因此您必须自己设置运行时动态链接器。
一个简单的包装脚本(如果您不想修改全局环境)如下:
#!/bin/bash
export DYLD_LIBRARY_PATH=/path/to/pg/lib 
exec rails "$@"

"$@"基本上意味着“将所有参数直接传递到此脚本中”。它可以正确保留引号,并且基本上意味着rails命令无法告诉您未直接运行它。

谢谢Craig。我不想全局设置DYLD_LIBRARY_PATH环境变量的原因是什么?如果包装脚本是最佳方法,那么我应该如何保存脚本以及在哪里?即使用什么文件名和扩展名以及在哪个目录中,这样Rails就知道在运行时运行脚本了? - stewartm
我尝试在SO /谷歌上搜索如何全局设置DYLD_LIBRARY_PATH,但找不到有意义的解决方案。我该怎么做?我想我需要将路径设置为/users/stewartmccoy/Library/PostgreSQL/9.2/,因为那是我安装pg的地方?如果我采取这种方法,我是否能够只使用rails s在本地主机上查看应用程序? - stewartm
最后一个问题...我通过在Gemfile中添加gem来解决了sqlite3适配器的问题...然而,当我再次运行rails s时,我又一次收到了DYLD_LIBRARY_PATH错误。有没有办法使更改永久生效,而不是每次都运行export DYLD_LIBRARY_PATH=/Users/stewartmccoy/Library/PostgreSQL/9.2/lib:$DYLD_LIBRARY_P‌​ATH - stewartm
2
对于后来者,我通过在VIM中编辑.bash_profile并添加export DYLD_LIBRARY_PATH=/Users/stewartmccoy/Library/PostgreSQL/9.2/lib:$DYLD_LIBRARY_P‌​ATH永久地解决了这个问题,然后在启动rails s之前重新启动了终端。 - stewartm
在我的情况下,库文件位于 /Library/PostgreSQL/9.3/lib。因此,我通过运行 export DYLD_LIBRARY_PATH=/Library/PostgreSQL/9.3/lib:$DYLD_LIBRARY_PATH 解决了这个问题。 - Pablo Marambio
显示剩余7条评论

6

我想在这里添加一些意见,回答前面的问题。

有时候只需要重新安装pg gem即可。

gem uninstall pg && gem install pg

在我的情况下,我将postgresql服务器从 9.6 升级到了 14.x,而 dylib 路径也发生了改变。

/opt/local/lib/postgresql96/libpq.5.dylib -> /opt/local/lib/postgresql14/libpq.5.dylib

该 gem 使用 pg_config 来确定到 /opt/local/lib/postgresql[版本] 文件夹(命名为 PKGLIBDIR)的路径。为了构建 gem 的本地库,rake 在编译时通过从 pg_config 抓取动态库的路径来设置路径。一旦 pg gem 被编译,这些路径将会永久链接。因此,重新安装会重新编译和重新链接到新的库。


在我的情况下,它甚至没有升级。pg-1.4.5只是被重新安装,然后它就可以工作了。很奇怪。 - michael_teter

2
一篇类似的stackoverflow答案建议从路径中删除Postgres.app,卸载pg宝石,并再次安装pg宝石。(我没有仔细检查是否适用于此问题,但它解决了我的一个问题,我想传播这个解决方案,因为我很难找到它。)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接