Adding “contrib” Extensions to MacPorts PostgreSQL
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.
- Install PostgreSQL (sudo port -k install postgresql83) or, if you’ve already installed it, re-install it (sudo port -nk upgrade --force postgresql83).
- 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.
- 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.
- 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.
- Clean the postgresql83 port: sudo port clean postgresql83.
Nice tip, thanks!
You don’t need to edit macports.conf first however; just install with ‘port -k install postgresql’.
Thanks for the tip about keep mode. I updated the post to simplify the steps.
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”
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.
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!
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.
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
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.)