Running Ruby in PostgreSQL on Mac OS X
No doubt, PostgreSQL is the best open-source database system. You may argue about that, but there’s nothing else for you to do than accept it :-). Ruby, on the other hand, is love at the first sight language, so it’s seems natural to combine those two and become comfortable happy, uhmm ;-). I’ll show you how to make this combination happen on OS X, compilation on other platforms is described somewhere else.
Compiled on: iMac 2.16 GHz, Intel Core 2 Duo, Mac OS X 10.4.9, Ruby 1.8.5 (2006-12-25 patchlevel 12) [i686-darwin8.8.1], PostgreSQL 8.1.8
Prerequisities: PostgreSQL installed from source (compiled – not difficult, and compiles smoothly), Ruby installed
- 1. get the PL/Ruby package by Guy Decoux.
- 2. ungzip the archive (double click will do, command-line fans – do whatever you like:-)
- 3. in Terminal enter the plruby-0.5.0 directory
- 4. type ruby extconf.rb --with-safe-level=0
- 5. type make
- 6. type sudo make install
- 7. now PL/Ruby is installed, we need to create some PostgreSQL hook before starting to write our stored procedures/functions in Ruby.
- 8. enter the template1 database, which is used as template for newly created databases. type psql template1
- 9. type create function plruby_call_handler() returns language_handler as '/usr/local/lib/ruby/site_ruby/1.8/i686-darwin8.8.1/plruby.bundle' language 'C';
- 10. create language inside the database create language 'plruby' handler plruby_call_handler lancompiler 'PL/Ruby';
- 11. quit the console \q, you are set.
Now you might want to create new database that will include PL/Ruby language. If you want to add Ruby support to existing database, simply repeat steps 9 to 11 for your database.
Let’s see some action, we will write one simple stupid function used as multiplier, written in Ruby. *Sorry folks, I don’t know how to disable typographic apostrophes in Wordpres, you have to replace ` to ‘ in the commands.*
psql template1
Welcome to psql 8.1.8, the PostgreSQL interactive terminal.
Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit
template1=# create database rubytest;
CREATE DATABASE
template1=# \c rubytest
You are now connected to database "rubytest".
rubytest=# create or replace function ruby_multiply(integer) returns integer as 'return 2*args[0].to_i' language 'plruby';
CREATE FUNCTION
rubytest=# select ruby_multiply(2);
ruby_multiply
----------
4
(1 row)
More to be found at: Installing untrusted PL/Ruby for PostgreSQL and PL/Ruby loves RubyGems and DRb by Robby Russel, and official PL/Ruby project homepage.
About this entry
You’re currently reading “Running Ruby in PostgreSQL on Mac OS X,” an entry on AprilChild
- Published:
- 05.10.07 / 10am
- Category:
- PostgreSQL, Ruby

No comments
Jump to comment form | comments rss [?] | trackback uri [?]