Ruby Postgres script

Swami Nikhilaananda said:
"Ruby is a gem of a language"

There was a very simple requirement that I had - I wanted to write a script to update a postgres table. On looking up quite a few resources also, I was unable to solve the issue that I was facing. Let me put down the completely stripped down version of the script here first...

#!/usr/bin/ruby
require 'postgres'
conn = PGconn.connect("localhost", 5432, '', '', "tblName", "username", "password")
puts conn
This script is the typical script that you will find on looking up any site. However, the error that you will face on running this is the following:
... `require': no such file to load -- postgres (LoadError) ...
This was quite annoying... Finally I discovered the required solution. So, I thought that this needs to be shared. Though it appears to be trivial, it is definitely not something that I could find with ease :)
#!/usr/bin/ruby
require 'rubygems' 
require 'postgres'
conn = PGconn.connect("localhost", 5432, '', '', "tblName", "username", "password")
puts conn
That line needs to come before you add require for others. Then it works like a charm <-- well, everyone uses that line :P So, thought I should too :-)


Do you spend a lot of time on the computer? Click here to learn how to use the Dvorak keyboard

Comments

Amod said…
Another way is to add run option '-rubygems' while running the file.

Popular posts from this blog

THE CURIOUS CASE OF RAHUL GANDHI - Nitin Gupta(Rivaldo)

The (fake) Quest To Eradicate AIDS with Mythical Mystical Indian roots

To each his own