JRuby + Postgresql + Sequel

Your life of Ruby + Java is going to be fun and painful. I hope this will get you started quickly so you can move on to the more annoying stuff. (JRuby is still amazing though. JVM is also quite lovely from what I've read.)

Required: Make sure you have a Java VM and Postgresql setup.

Download JRuby 1.3.1. (Archived. Already built.):
http://kenai.com/projects/jruby/pages/Home

Extract the contents.

Go to that folder:
cd jruby-1.3.1

Check everything is ok:
bin/jruby -v

Now do:
bin/jruby -S gem install sequel

Now go to: http://jdbc.postgresql.org/download.html
Download a driver (.jar file) where it says "Current Version."

Remember the location.

Now to test if it works:

# FILE NAME:  quick_start.rb
require 'rubygems'

require '/Path/To/Driver/postgres-..-..jdbc4.jar' # Put the path/filename of the driver you downloaded.

require 'sequel'

host = 'localhost'
db_name = 'my-database' # <=== Change this.
user = 'my-username' # <=== Change this.
pass = 'my-password' # <=== Change this.
DB = Sequel.connect( "jdbc:postgresql://%s/%s?user=%s&password=%s" % [ host, db_name, user, pass] )

my_table = :schema_info # <=== Change this.

puts DB[ my_table ].all.inspect

Now do:
bin/jruby ~/Path/To/quick_start.rb

If it works, you can go and properly install and integrate JRuby into your system: http://kenai.com/projects/jruby/pages/Home
(Go to:  "JRuby Basics" > "Getting Started")
There is also: "JRuby Basics" > "Walkthroughs and Tutorials"

COMMON MISTAKES:

  • The equlivalent of "gem install" is "jruby -S gem" (with a capital 'S')
  • Write a proper Sequel connection string: jdbc:postgresql://HOST/DB?user=USER&password=PASS
    Notice how it differs from other connections strings in regular Ruby + Sequel.
    More info. on Sequel + JRuby + connection strings: http://sequel.rubyforge.org/rdoc/classes/Sequel/JDBC.html
  • An alternative to downloading the .jar/jdbc driver for postgresql is to install the JDBC-Postgresql gem:
    bin/jruby -S gem install jdbc-postgres
    (It uses a slightly older JDBC driver. It's compatiable for Postgresql 8.2).

Eventually, I gave up JRuby for Ruby Enterprise Edition. http://www.rubyenterpriseedition.com/

Posted 3 months ago

0 comments

Leave a comment...

 
Got an account with one of these? Login here, or just enter your comment below.
Posterous-login    Connect    twitter