Rails/Ruby/Postgres - LoadError cannot load such file -- pg_ext
Asked Answered
P

3

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

Pokpoke answered 30/7, 2012 at 6:47 Comment(4)
have you gem installed the 'pg' gem with dependencies?Galvanoscope
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'Pokpoke
Aren't you supposed to add gem 'pg' to the Gemfile in Rails and install all gems with dependencies by running bundle install?Near
see this tread #15148377Cooking
V
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.

Vendible answered 15/6, 2013 at 11:51 Comment(0)
C
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.

Coextensive answered 17/3, 2015 at 9:35 Comment(0)
A
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

Attend answered 11/12, 2012 at 6:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.