10

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

4
  • have you gem installed the 'pg' gem with dependencies? Commented Jul 30, 2012 at 8:19
  • I'm not sure if I installed the gem with dependencies. When I initially installed the gem I used the following command: gem install pg -- --with-pg-config='/usr/local/PostgreSQL/9.1/bin/pg_config' Commented Jul 30, 2012 at 14:15
  • 2
    Aren't you supposed to add gem 'pg' to the Gemfile in Rails and install all gems with dependencies by running bundle install? Commented Dec 5, 2012 at 8:18
  • see this tread stackoverflow.com/questions/15147877/… Commented May 11, 2023 at 18:49

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.