1

I am trying to connect to pg like such

require 'pg'

db1_params = {
  host: 'DBHOST',
  dbname: 'DBNAME',
  user: 'DBUSER',
  password: 'DBPASSWORD',
  port: 5432
}

db1 = PG::Connection.new(db1_params)

orders = db1.exec_params('SELECT count(*) from orders')

puts orders

But i am getting `initialize': could not connect to server: Operation timed out (PG::ConnectionBad) error. My main goal is to initialize db1 and db2 and count orders rows on both and match the total. Is there any better way of achieving this? Its not just one table, there are 150 tables and i need to count rows on all tables on both db and match the results.

suyesh
  • 530
  • 7
  • 23
  • Also got it working by removing port. weird. but now i am getting output like such "#" i need counts when i send count method on the object it give me one, thats not correct. – suyesh Oct 11 '16 at 15:09
  • try `orders.values` – Abdoo Dev Oct 11 '16 at 15:13
  • 2
    Rather than talk directly to a DBM, it's highly recommended to take advantage of an ORM. I like Sequel, which is developed first for PostgreSQL, but Active Record is very capable. The advantage to an ORM is it makes it very easy to move between DBMs without having to rewrite your code or queries. Modern ORMs generate excellent queries. – the Tin Man Oct 11 '16 at 16:51

0 Answers0