Installing PostgreSQL and DBD::Pg on OSX

PostgreSQL is easy available for OSX thanks to the packages distributed officially by PostgreSQL itself. But if you got a 64-bit version of Perl installed (you probably have if you got a relatively new Intel Mac), DBD::Pg simply won't compile unless you manage to fake its build environment so that it think it will be compiling against a 32-bit Perl installation.
Different howtos available explains how to do this, however, it was not possible with the latest OSX and Xcode release that we had available on our system. The error message we get again and again is:

dyld: lazy symbol binding failed: Symbol not found: _PQconnectdb
Referenced from: /opt/local/lib/perl5/site_perl/5.10.1/darwin-2level/auto/DBD/Pg/Pg.bundle
Expected in: dynamic lookup

So. The quick fix is to remove PostgreSQL installed from the official package repo and instead install it from Macports. By doing so, PostgreSQL will be 64-bit and have the correct header files which is necessary for the DBD::Pg module to compile.

This should work with the Perl 5.10 installation shipped with OSX, but you might want to install your own Perl environement in your home directory first

Unless you don't already have Macports installed, do it.

1. Install postgresql by issuing the following command in terminal:

# sudo port install postgresql84-server

2. Install the database

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

3. The Macport port of PostgreSQL does not include a default startup script except pg_ctl. I just made a simple wrapper around it (e.g. ~/bin/ that look like this:


case "$1" in
    echo "Starting... "
    sudo su postgres -c "/opt/local/lib/postgresql84/bin/pg_ctl -D $DATADIR start"
    echo "Stopping... "
    sudo su postgres -c "/opt/local/lib/postgresql84/bin/pg_ctl -D $DATADIR stop"
    echo "Reloading... "
    sudo su postgres -c "/opt/local/lib/postgresql84/bin/pg_ctl -D $DATADIR reload"
    echo "Usage $0 [start|stop|reload]

4. Set some environment vars in your current shell:

# export POSTGRES_LIB=/opt/local/lib/postgresql84/

5. Install DBD::Pg

# cpan -i DBD::Pg

Thats it.

Last tweets

Follow tagestad on Twitter