PostgreSQL, MySQL and Ruby Drivers with MacPorts

If you’re on MacOS X and you’re developing database aware applications you certainly want to have installed locally one or both of those populars RDBMS : PostgreSQL or MySQL.

My prefered way of doing it is through MacPorts (previously known as DarwinPorts), a package manager designed initially for Darwin and now focusing on MacOS X.

I assume you already have installed MacPorts as explain on their website.

So let’s open a console with your favorite application (I tend to prefer iTerm over and enter those commands :

sudo port install mysql5 +server

this will install MySQL 5.x database and the launchd script needed to start it (on-demand or at startup).
When the installation is done you’ll be asked to install the launchd script, just ignore this part and jump to the initialization part.

To initialize MySQL and secure access to it, just enter those commands

sudo -u mysql mysql_install_db5
sudo -u mysql /opt/local/lib/mysql5/bin/mysqld_safe &
/opt/local/lib/mysql5/bin/mysqladmin -u root password 'your_password'

You should now have a shinny new MySQL 5 installation with a password for your root account.

Then PostgreSQL, let’s type in :

sudo port install postgresql81-server

this will install PostgreSQL 8.1.x database and its launchd script.
When the installation is done, as with MySQL skip the launchd part and go to the initialization by issuing those commands :

sudo mkdir -p /opt/local/var/db/postgresql81/defaultdb
sudo chown postgres81:postgres /opt/local/var/db/postgresql81/defaultdb
sudo su postgres81 -c '/opt/local/lib/postgresql81/bin/initdb -D /opt/local/var/db/postgresql81/defaultdb'

PostgreSQL access authorization is something beyond this article, so I’ll don’t cover it.

If all goes well you should now have 2 working installations, but no simple way to start/stop them. A simple and powerful way of handling this (and launchd scripts, create/edit/load/unload) is Lingon. Just download the last version and go to the “Users Daemons” tab and you will see your two launchd scripts are there waiting for you to load/unload/enable/disable them.

To finish this installation, if you’re a Ruby kind of guy you probably want to access those databases from it. There are two ways to install the needed libraries : MacPorts or RubyGems.

For MySQL, there is no problem with MacPorts just type :

sudo port install rb-mysql

If you prefer the RubyGems solution the command is little more tricky :

sudo gem install mysql -- --with-mysql-config=/opt/local/bin/mysql_config5

For MacOS X.5 (Leopard) stock Ruby 1.8.6 :

sudo env ARCHFLAGS="-arch i386" gem install mysql -- --with-mysql-config=/opt/local/bin/mysql_config5

For PostgreSQL, you can’t do it through MacPorts unless you choose to install postgresql8 (and not postgresql81), because of the dependencies. But you can install the library through RubyGems with :

 sudo gem install postgres -- --with-pgsql-include-dir=/opt/local/include/postgresql81/ --with-pgsql-lib-dir=/opt/local/lib/postgresql81/ 

For MacOS X.5 (Leopard) stock Ruby 1.8.6 :

 sudo env ARCHFLAGS="-arch i386" gem install postgres -- --with-pgsql-include=/opt/local/include/postgresql81/ --with-pgsql-lib=/opt/local/lib/postgresql81/ 

This is a simple and yet functional setup to work.

Sun, 19 Nov 2006 13:06 Posted in , ,

  1. By Grant Hutchins 10/04/2007 at 03h26

    Thanks! It works like a charm.

    By the way when I was a college student here in America I got the chance to spend a month in Metz, and I really enjoyed it. I was at Georgia Tech Lorraine for a special month-long program.

  2. By Tron Jonathan 10/04/2007 at 10h02

    Grant, great to hear this helped someone else.

    Glad you loved your month in Metz, it’s a really great city.

  3. By John C 22/05/2007 at 08h12

    Many thanks for the macports postgresql/mysql integration for ruby gems.

  4. By Foobar 23/05/2007 at 10h38

    Why is mysql so much more popular with rails development? Is the pgsql adapter much slower since its not that maintained?

  5. By Jonathan Tron 23/05/2007 at 21h25

    Foobar : I don’t think it’s related to speed or update of adapters, but certainly due to the administration side of both sgbd. MySQL is simpler for a non dbadmin to maintain up and running than PostgreSQL.
    Another point, I think, is the rails philosophy to handle data relations/integrity in app and not db. Personally I tend to prefer PostgreSQL for big databases with lots of data and complex relations, but for small to mid-size (and some big) databases MySQL do very well.

  6. By Constantin Miroslav 19/04/2008 at 23h09

    I was getting the following error:

    extconf.rb:73: command not found: pg_config —bindir
    extconf.rb:46: command not found: pg_config —includedir
    extconf.rb:53: command not found: pg_config —libdir
    checking for main() in -lpq… no

    • extconf.rb failed *

    I had to change the line for gem install postgres to:

    sudo env ARCHFLAGS=“-arch i386” gem install postgres — —with-pgsql-include=/opt/local/include/postgresql81/ —with-pgsql-lib=/opt/local/lib/postgresql81/
  7. Thanks

  • By Rikas 30/10/2008 at 12h17

    Same problem here!!!

  • By Jonathan Tron 30/10/2008 at 12h35

    Miroslav, Rikas : thanks this is corrected now

  • Comment PostgreSQL, MySQL and Ruby Drivers with MacPorts