简体   繁体   中英

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

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

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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