Rails/Ruby/Postgres - LoadError cannot load such file — pg_ext

假如想象 提交于 2019-12-08 17:26:24

问题


I am trying to call a Ruby script (which connects to a postgres db) using the rails controller below, however it appears it is having problems loading one of the PG gem files. I have set my require statement to require 'pg' and tried the absolute path as well (require /usr/local/rvm/gems/ruby-1.9.3-p194@railsTest/gems/pg-0.14.0/lib/pg/). The file 'pg_ext' is in fact present in the directory. Additionally, I can run the ruby script standalone without any problems (dbrubyscript.rb), however when rails is added to this equation it craps out with a cannot load such file -- pg_ext error.

Any direction here would be much appreciated as I have not been able to find anything online that fixes this issue

Rails controller:

class TestdlController < ApplicationController
def runmyscript
  load "/usr/local/rvm/my_app/ruby_scripts/reports/dbrubyscript.rb"
  send_file '/usr/local/rvm/tmp/failedtests.csv', :type => 'text/csv', :disposition => 'inline'
  flash[:notice] = "Reports are being processed..."
end
end

.rb file (dbrubyscript.rb) has the following:

require 'rubygems'
require 'pg'

connects to (production) database
@conn = PGconn.connect("zzzzz.test.prod", 5432,"","","yyyyy_prod" ,"postgres", "xxxxxx")
.....

Trace Error: LoadError in TestdlController#runmyscript

cannot load such file -- pg_ext

Rails.root: /usr/local/rvm/my_app Application Trace | Framework Trace | Full Trace app/controllers/Testdl_controller.rb:3:in `runmyscript'

This error occurred while loading the following files:
/usr/local/rvm/my_app/ruby_scripts/reports/dbrubyscript.rb
/usr/local/rvm/gems/ruby-1.9.3-p194@railsTest/gems/pg-0.14.0/lib/pg/
pg_ext


回答1:


Try running ruby /usr/local/rvm/gems/ruby-1.9.3-p194@railsTest/gems/pg-0.14.0/lib/pg/ext/extconf.rb and see what errors you get. That helped me determine that in my case, my PostgreSQL client was too old. Your error may be different since you appear to have a current-ish versioninstalled.




回答2:


I had the same problem. I have a stand alone ruby script. It connects via pg to postgres and worked if I ran it directly from the shell.

If I tried to run it via rspec I get the Error cannot load such file -- pg.

Solved: The problem for rspec was, the pg gem was not defined in the Gemfile. After put pg into Gemfile, and retestet via rspec, it worked.




回答3:


Try adding a line to your Gemfile:

gem "pg"

Then run bundler via the command line:

bundle install

Rails uses Bundler to manage your gems and dependencies. You can read a bit more about the idea behind Bundler here: http://gembundler.com/v1.2/rationale.html



来源:https://stackoverflow.com/questions/11716532/rails-ruby-postgres-loaderror-cannot-load-such-file-pg-ext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!