# Using ActiveRecord in a Ruby script not working

**URL:** <https://www.ruby-forum.com/t/using-activerecord-in-a-ruby-script-not-working/115006>\
**Category:** Rails\
**Created:** [September 19, 2007, 5:49pm UTC](https://www.ruby-forum.com/t/using-activerecord-in-a-ruby-script-not-working/115006 "2007-09-19T17:49:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![happyraul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ecc23a/32.png) [@happyraul](https://www.ruby-forum.com/u/happyraul)\
**Post date:** [September 19, 2007, 5:49pm UTC](https://www.ruby-forum.com/t/using-activerecord-in-a-ruby-script-not-working/115006/1 "2007-09-19T17:49:57Z")

</div>

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](http://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!

---

<div class="post-metadata">

**Author:** ![happyraul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ecc23a/32.png) [@happyraul](https://www.ruby-forum.com/u/happyraul)\
**Post date:** [September 19, 2007, 11:00pm UTC](https://www.ruby-forum.com/t/using-activerecord-in-a-ruby-script-not-working/115006/2 "2007-09-19T23:00:25Z")

</div>

On 9/19/07, happyraul [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.?

---

<div class="post-metadata">

**Author:** ![happyraul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ecc23a/32.png) [@happyraul](https://www.ruby-forum.com/u/happyraul)\
**Post date:** [September 19, 2007, 11:46pm UTC](https://www.ruby-forum.com/t/using-activerecord-in-a-ruby-script-not-working/115006/3 "2007-09-19T23:46:32Z")

</div>

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:in`new’  
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: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

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