Simple example of Postgres query in Ruby

From the pg gem documentation (http://rubydoc.info/gems/pg/0.10.0/frames)

require 'pg'
conn = PGconn.open(:dbname => 'test')
res  = conn.exec('SELECT 1 AS a, 2 AS b, NULL AS c')
res.getvalue(0,0) # '1'
res[0]['b']       # '2'
res[0]['c']       # nil

My next question would be authentication with a DB that requires a password.
Looks like you can send a connection string like this:
PGconn.connect( "dbname=test password=mypass")
or use the constuctor with parameters:
PGconn.new(host, port, options, tty, dbname, login, password) or use a hash like :password => '...' see here for all available options.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)