Using ActiveRecord in a Ruby script not working

Greetings! I am trying to use ActiveRecord in a non-Rails script to
access a database, but I’m not doing something right apparently:

require ‘rubygems’
require ‘active_record’

ActiveRecord::Base.establish_connection(
:adapter => “postgresql”,
:host => “myhost.org”,
:port => 1234,
:username => “myuser”,
:password => “mypass”,
:database => “mydb”
)

conn = ActiveRecord::Base.connection

puts conn.active?

When I try to run this code, I get an exception when trying to
retrieve the connection:

:~/ruby$ ruby activerecordtest.rb
/usr/lib/ruby/1.8/readbytes.rb:23:in readbytes': End of file reached (EOFError) from /usr/lib/ruby/gems/1.8/gems/postgres-pr-0.4.0/lib/ postgres-pr/message.rb:32:in read’
from /usr/lib/ruby/gems/1.8/gems/postgres-pr-0.4.0/lib/
postgres-pr/connection.rb:30:in initialize' from /usr/lib/ruby/gems/1.8/gems/postgres-pr-0.4.0/lib/ postgres-pr/connection.rb:29:in loop’
from /usr/lib/ruby/gems/1.8/gems/postgres-pr-0.4.0/lib/
postgres-pr/connection.rb:29:in initialize' from /usr/lib/ruby/gems/1.8/gems/postgres-pr-0.4.0/lib/ postgres-pr/postgres-compat.rb:23:in new’
from /usr/lib/ruby/gems/1.8/gems/postgres-pr-0.4.0/lib/
postgres-pr/postgres-compat.rb:23:in initialize' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/connection_adapters/postgresql_adapter.rb:24:in connect’
from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/
active_record/connection_adapters/postgresql_adapter.rb:24:in
postgresql_connection' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/connection_adapters/abstract/connection_specification.rb: 262:in send’
from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/
active_record/connection_adapters/abstract/connection_specification.rb:
262:in connection_without_query_cache=' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/query_cache.rb:54:in connection=’
from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/
active_record/connection_adapters/abstract/connection_specification.rb:
230:in retrieve_connection' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/connection_adapters/abstract/connection_specification.rb: 78:in connection’
from activerecordtest.rb:13

Anyone have any ideas why this isn’t working?

Thanks!

On 9/19/07, happyraul [email protected] wrote:

:port => 1234,
retrieve the connection:

:~/ruby$ ruby activerecordtest.rb
/usr/lib/ruby/1.8/readbytes.rb:23:in `readbytes’: End of file reached
(EOFError)

1234 is not the standard PostgreSQL port. Are you sure that’s right,
and that the postmaster is running, etc.?

Yes, 1234 is not the actual port I’m using, I’ve obscured all
information in the establish_connection method except that it is a
postgresql DB that I’m trying to use. Anyway, the actual port that I
am using is correct, for I am able to use this database server through
an actual Rails project, and in my yml config, the port I have
specified there is the same that I tried to use in this script, which
did not work. Actually, here is the output if I use the incorrect
port, with the same script:

$ ruby activerecordtest.rb
/usr/lib/ruby/gems/1.8/gems/postgres-pr-0.4.0/lib/postgres-pr/
connection.rb:138:in initialize': Connection refused - connect(2) (Errno::ECONNREFUSED) from /usr/lib/ruby/gems/1.8/gems/postgres-pr-0.4.0/lib/ postgres-pr/connection.rb:138:innew’
from /usr/lib/ruby/gems/1.8/gems/postgres-pr-0.4.0/lib/
postgres-pr/connection.rb:138:in establish_connection' from /usr/lib/ruby/gems/1.8/gems/postgres-pr-0.4.0/lib/ postgres-pr/connection.rb:25:ininitialize’
from /usr/lib/ruby/gems/1.8/gems/postgres-pr-0.4.0/lib/
postgres-pr/postgres-compat.rb:23:in new' from /usr/lib/ruby/gems/1.8/gems/postgres-pr-0.4.0/lib/ postgres-pr/postgres-compat.rb:23:ininitialize’
from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/
active_record/connection_adapters/postgresql_adapter.rb:24:in
connect' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/connection_adapters/postgresql_adapter.rb:24:inpostgresql_connection’
from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/
active_record/connection_adapters/abstract/connection_specification.rb:
262:in send' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/connection_adapters/abstract/connection_specification.rb: 262:inconnection_without_query_cache=’
from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/
active_record/query_cache.rb:54:in connection=' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/connection_adapters/abstract/connection_specification.rb: 230:inretrieve_connection’
from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/
active_record/connection_adapters/abstract/connection_specification.rb:
78:in `connection’
from activerecordtest.rb:13

So as you can see, that is not the problem.