SOA / Web Services / Java

A Technology Blog

Ruby: Working with Postgres Database

Posted by Vivek on June 10, 2015

Firstly, install a ruby gem pg using the following command

gem install  pg

Then write a class to connect to database and retrieve values from a table

class PostGresClient

#!/usr/bin/env ruby

require ‘pg’

# Output a table of current connections to the DB
conn = PG.connect(“localhost”, 5432, , , “”, “”, “”)
res = conn.exec( “SELECT * FROM public.\”Employee\”)
res.each do |row|
row.each do |column|
puts column
end
end

end

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: