简体   繁体   English

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

[英]Ruby on Rails / PostgreSQL - Library not loaded error when starting server

Heyo.嘿嘿。 I apologize in advance if this question has been answered in another thread.如果这个问题已经在另一个线程中得到回答,我提前道歉。 I've searched through the site but I didn't find an answer.我已经搜索了该网站,但没有找到答案。 However, the closest related issue was Ruby on Rails / PostgreSQL - Library not Loaded error when starting server- libq.5.dylib , but it was never resolved, and the answer doesn't help me.但是,最接近的相关问题是Ruby on Rails / PostgreSQL - Library not Loaded error when started server- libq.5.dylib ,但从未解决,答案对我没有帮助。

I'm trying to get PostgreSQL running on my Mac OSX 10.6.8.我试图让 PostgreSQL 在我的 Mac OSX 10.6.8 上运行。 I already have Ruby 1.9.3p392 and Rails 3.2.13 installed.我已经安装了 Ruby 1.9.3p392 和 Rails 3.2.13。

Since I've 10.6.8, and PostGresApp requires 10.7+, I tried installing PG using the manual installer (v9.2.3) available here: http://www.enterprisedb.com/products-services-training/pgdownload由于我有 10.6.8,而 PostGresApp 需要 10.7+,我尝试使用此处提供的手动安装程序 (v9.2.3) 安装 PG: http://www.enterprisedb.com/products-services-training/pgdownload

I first installed it to its default path at: /Library/PostgreSQL/9.2/.我首先将它安装到默认路径:/Library/PostgreSQL/9.2/。 I then installed the pg gem ( PATH=$PATH:/Library/PostgreSQL/9.2/bin sudo gem install pg ).然后我安装了 pg gem( PATH=$PATH:/Library/PostgreSQL/9.2/bin sudo gem install pg )。 When I tried rails s to launch my localhost, I got the error below.当我尝试使用rails s启动我的 localhost 时,出现以下错误。

After some reading, I guessed it might be a path error, so I removed the pg gem, and then used the postgresql uninstaller.经过一番阅读,我猜测可能是路径错误,所以我删除了 pg gem,然后使用了 postgresql 卸载程序。 I then reinstalled pg in /users/stewartmccoy/Library/PostgreSQL/9.2/...and then reinstalled the pg gem.然后我在 /users/stewartmccoy/Library/PostgreSQL/9.2/... 中重新安装了 pg,然后重新安装了 pg gem。 But I still get the same error.但我仍然得到同样的错误。

Any thoughts on how I might correctly install PostgreSQL and get my rails server running?关于如何正确安装 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>'

The key part of the error is: 错误的关键部分是:

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

This suggests that ruby can't find libpq at runtime. 这表明ruby在运行时找不到libpq To address that you should probably set the DYLD_LIBRARY_PATH environment variable to point to the lib directory of your PostgreSQL install, either globally or in a wrapper script you use to start Rails. 为了解决这个问题,您应该将DYLD_LIBRARY_PATH环境变量设置为指向PostgreSQL安装的lib目录,无论是全局还是用于启动Rails的包装脚本。 See this superuser question for some more info. 有关更多信息,请参阅此超级用户问题

The Pg gem can find the library during compilation and installation because the pg_config executable is on the PATH and it uses that to find libpq . Pg gem可以在编译和安装期间找到库,因为pg_config可执行文件位于PATH ,并使用它来查找libpq It appears that it doesn't store the path for use at runtime so you have to set the runtime dynamic linker up yourself. 它似乎不存储在运行时使用的路径,因此您必须自己设置运行时动态链接器。

A simple wrapper script (in case you don't want to modify your global environment) is something like: 一个简单的包装脚本(如果你不想修改你的全局环境)是这样的:

#!/bin/bash
export DYLD_LIBRARY_PATH=/path/to/pg/lib 
exec rails "$@"

The "$@" basically means "pass all arguments to this script through as if they were passed here directly". “$ @”基本上意味着“将所有参数传递给此脚本,就好像它们是直接传递给这里一样”。 It preserves quoting correctly and essentially means that the rails command can't tell you didn't run it directly. 它保留了正确的引用,实质上意味着rails命令无法告诉你没有直接运行它。

A similar stackoverflow answer proposed removing Postgres.app from your PATH, uninstalling the pg gem, and installing the pg gem again. 类似的stackoverflow答案建议从你的PATH中删除Postgres.app,卸载pg gem,然后再次安装pg gem。

(I'm not taking the time to check carefully if that applies to this problem, but it solved a problem I had and I want to spread that solution around since I had trouble finding it.) (我没有花时间仔细检查这是否适用于这个问题,但它解决了我遇到的问题,因为我无法找到它,所以我想传播解决方案。)

I wanted to put my two cents here to answers above.我想把我的两分钱放在上面的答案上。

Sometimes it's enough to just reinstall pg gem.有时只需重新安装pg gem 就足够了。

gem uninstall pg && gem install pg

In my case I upgraded postgresql server from 9.6 up to 14.x and dylib paths were changed.就我而言,我将 postgresql 服务器从9.6升级到14.x ,并且更改了 dylib 路径。

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

The gem uses pg_config to determine paths to /opt/local/lib/postgresql[version] folder (named PKGLIBDIR ). gem 使用pg_config来确定/opt/local/lib/postgresql[version]文件夹(名为PKGLIBDIR )的路径。 To build native library for the gem rake sets paths to dynamic libs by fetching them from pg_config in compile.要为 gem rake 构建本机库,通过在 compile 中从pg_config中获取动态库来设置动态库的路径。 Once pg gem is compiled the paths are linked forever.编译pg gem 后,路径将永远链接。 So, the reinstallation recompiles and relinks path to the new libraries.因此,重新安装会重新编译并将路径重新链接到新库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM