Adding “contrib” Extensions to MacPorts PostgreSQL

Posted by on Aug 5, 2010 in Blog | Tags: , | 8 Comments

PostgreSQL is my database of choice. The main reason is that a standard distribution comes with just about everything one could ask for in a free RDBMS, and if what you want isn’t already installed, it’s often in the “contrib” directory. In particular, now that I’m doing a lot of iOS development, and location awareness is a big part of mobile apps, it’s useful to be able to work with coordinates. PostgreSQL does this using the cube data type and earthdistance functions, both in “contrib”.

MacPorts, however, doesn’t include much of the contents of “contrib” in a standard install, including both of these extensions. This makes it a lot harder to develop code using them unless I want to set up a development database on my production server, something I am not eager to do.

Thankfully, there is a solution: disable auto-clean, and after installing the postgresql83 (or 84, depending on the version you want), you can install the extra “contrib” extensions by hand.

  1. Install PostgreSQL (sudo port -k install postgresql83) or, if you’ve already installed it, re-install it (sudo port -nk upgrade --force postgresql83).
  2. Change into MacPorts working directory: cd `port work postgresql83`/postgresql-8.3.11/contrib. Your specific version will differ from mine if you’re reading this more than a few months after I wrote it.
  3. For each extension you want installed, cd into the directory, make all, then sudo make install. This compiles C code, if any, into a shared library, then copies the library and supporting SQL files (to create types, functions, etc., and also to drop them) to /opt/local/share/postgresql83/contrib.
  4. Change into /opt/local/share/postgresql83/contrib and run the SQL files associated with the extensions you want. This probably needs to be done as the PostgreSQL super-user. For example, psql -U postgres dbname <cube.sql.
  5. Clean the postgresql83 port: sudo port clean postgresql83.

8 Comments

  1. Richard Michael
    December 28, 2010

    Nice tip, thanks!

    You don’t need to edit macports.conf first however; just install with ‘port -k install postgresql’.

    • Steve
      December 28, 2010

      Thanks for the tip about keep mode. I updated the post to simplify the steps.

  2. perrog
    January 15, 2011

    Great tip, thanks!
    I needed update to build the specific contrib.
    Note! specifying “sudo port -k upgrade –force postgres83″ also rebuild ports depending upon port postgres83. You probably just want “sudo port -nk upgrade –force postgresql83″

    • Steve
      January 18, 2011

      You make a very good point about “-n” and ignoring dependencies. I’m setting up a new environment now and it takes quite a bit longer without it! I updated the post to include your suggestion.

  3. Dilapidus
    June 7, 2011

    What a lifesaver. It’s non-trivial to go out and find the cube code and get it to compile.

    This saved my MusicBrainz install.

    Thanks!

  4. Tim Holahan
    July 10, 2011

    Thanks for this very helpful information.

    I found that I didn’t have to reinstall pg, I could just re-build it:

    sudo port build postgresql90

    That should save a bit of time if your installation is current.

  5. John
    April 22, 2012

    There’s actually a much more simple way — you can just tell macports to build it itself, by making a small edit to its Portfile: http://stackoverflow.com/a/9343432/168143

    • Steve Madsen
      April 23, 2012

      That is much simpler. Thanks!

      (Personally, I’ve switched to Homebrew and this is a non-issue there since it builds and installs all the contrib packages by default.)