<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Robby on Rails: Installing untrusted PL/Ruby for PostgreSQL</title>
    <link>http://www.robbyonrails.com/articles/2005/08/22/installing-untrusted-pl-ruby-for-postgresql</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>thoughts.sort_by{|t| t[:topic]}.collect </description>
    <item>
      <title>Installing untrusted PL/Ruby for PostgreSQL</title>
      <description>&lt;p&gt;This is going to be short and sweet.&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;&amp;#8220;PL/Ruby is a loadable procedural language for the Postgres database system that enable the Ruby language to create functions and trigger procedures&amp;#8221;&lt;/strong&gt;&lt;/p&gt;


	&lt;p&gt;&lt;b&gt;Method 1&lt;/b&gt;. The standard, safe, PL/Ruby.&lt;/p&gt;


Before running this, you need to have all the PostgreSQL headers installed. (se &lt;span class="caps"&gt;INSTALL&lt;/span&gt; in the postgresql directory)
&lt;code&gt;make install-all-headers&lt;/code&gt;

	&lt;p&gt;To install PL/Ruby, you need to download the tarball from &lt;a href="http://raa.ruby-lang.org/list.rhtml?name=pl-ruby"&gt;here&lt;/a&gt;.   As you can see, I download it with &lt;code&gt;wget&lt;/code&gt; and then install like I would any ruby library. (maybe plruby could become a gem?)&lt;/p&gt;


&lt;code&gt;
&lt;pre&gt;
cd /usr/local/src
wget ftp://moulon.inra.fr/pub/ruby/plruby.tar.gz
tar zxvf plruby.tar.gz
cd plruby
ruby extconf.rb
make
make install
&lt;/pre&gt;
&lt;/code&gt;

	&lt;p&gt;&lt;b&gt;Method 2&lt;/b&gt;: The untrusted, but super cool PL/Ruby.&lt;/p&gt;


	&lt;p&gt;Guy Decoux, author of PL/Ruby, was kind enough to share a secret about the PL/Ruby install. (from his email&amp;#8230;)&lt;/p&gt;


&lt;pre&gt;
Well plruby normally run with $SAFE = 12, this value if fixed at compile time. 

Now it has an undocumented option, if you compile it with

ruby extconf.rb --with-safe-level=0 ...

 it will run with $SAFE = 0 and you have the equivalent of an untrusted language.
&lt;/pre&gt;

	&lt;p&gt;Pretty simple solution, eh?&lt;/p&gt;


	&lt;p&gt;On my server I was able to run the following:&lt;/p&gt;


&lt;code&gt;
&lt;pre&gt;
cd /usr/local/src
wget ftp://moulon.inra.fr/pub/ruby/plruby.tar.gz
tar zxvf plruby.tar.gz
cd plruby
sudo ruby extconf.rb \ --with-pgsql-dir=/usr/local/pgsql-8.0 \ --with-safe-level=0 \ --with-suffix=u
make
make install
&lt;/pre&gt;
&lt;/code&gt;

	&lt;p&gt;&lt;strong&gt;Update: the &lt;code&gt;--with-suffix=u&lt;/code&gt; was added after someone commented on this. This allows you to install plruby and plrubyu.&lt;/strong&gt;&lt;/p&gt;


	&lt;p&gt;&lt;b&gt;Installing PL/Ruby in PostgreSQL&lt;/b&gt;
Up until now, you haven&amp;#8217;t actually installed the language into the database. We&amp;#8217;re close though!&lt;/p&gt;


	&lt;p&gt;All that you need to do is run the following commands to install it to a specific database in your server.&lt;/p&gt;


&lt;code&gt;
&lt;pre&gt;
$ psql template1
template1=# CREATE DATABASE plruby;
CREATE DATABASE
template1=# \c plruby
You are now connected to database "plruby".
plruby=#    create function plruby_call_handler() returns language_handler
plruby-#    as '/usr/lib/site_ruby/1.8/i386-linux/plruby.so'
plruby-#    language 'C';
CREATE FUNCTION
plruby=#    create  language 'plruby'
plruby-#    handler plruby_call_handler
plruby-#    lancompiler 'PL/Ruby';
CREATE LANGUAGE
plruby=#
&lt;/pre&gt;
&lt;/code&gt;

	&lt;p&gt;That should be all there is to it!&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;Where do we go from here?&lt;/strong&gt;&lt;/p&gt;


	&lt;p&gt;See my post: &lt;a href="http://www.robbyonrails.com/articles/2005/08/22/pl-ruby-loves-rubygems-and-drb"&gt;PL/Ruby loves RubyGems and DRb&lt;/a&gt;&lt;/p&gt;
</description>
      <pubDate>Mon, 22 Aug 2005 08:55:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:057f1e2834e458246b6752e4328325b1</guid>
      <author>Robby Russell</author>
      <link>http://www.robbyonrails.com/articles/2005/08/22/installing-untrusted-pl-ruby-for-postgresql</link>
      <category>Ruby</category>
      <category>PostgreSQL</category>
      <category>plruby</category>
      <category>ruby</category>
      <category>postgresql</category>
    </item>
  </channel>
</rss>
