Getting Django to run along side PostGIS (used for GeoDjango) can be tricky. A lot of the difficulties come down to versions not playing well with one another.
Here's what finally worked for me.
Step 1: Use Homebrew
The first thing I want to mention is that I'm not sure I could have actually accomplished this without Homebrew. It really is a great package management tool for OS X. For one thing, it handles dependencies much better than anything I've used in the past. Moreover though, it does a great job of not requiring you do sudo every damn thing you ever have to do at a terminal.
I'd also like to mention that when I first tried to start using Homebrew (about 2 years ago, I think) there were some issues because I had already sudo make installed a bunch of stuff. Homebrew couldn't seem to quite take full command of managing my packages and I always felt like I ended up with a half-baked install. I'm happy to report though that whatever issues I was having at that time seem to have been resolved. This time around, Homebrew was able to hijack all my libs and whatnot without any trouble.
So, if you've been avoiding using a package manager, bite the bullet and get Homebrew installed. It's pretty awesome.
The key to all of this is to understand what versions of each thing you're going to need. The biggest restriction is that Django's contrib.gis module doesn't support the latest PostGIS version (at least in earlier versions of Django). This means that if you just do a "brew install postgis", you'll get an unsupported version of PostGIS.
Instead, you should install PostGIS 1.5.x.
Unfortunately, the latest version(s) of PostgreSQL don't support PostGIS dating that far back. So now you need PostgreSQL 9.0.x.
So, to sum up, you'll want to get these installed:
- PostgreSQL 9.0.x
- PostGIS 1.5.x
So, here's how you get those installed with Homebrew:
brew tap homebrew/versions brew install postgis15 brew install postgresql9 brew untap homebrew/versions brew install gdal geos
Now, if you've already got different versions of PostGIS and PostgreSQL installed, you're going to need to either uninstall them or relink to the correct versions. Homebrew can help with that or you can do it manually.You can check out Homebrew's Wiki for some guidance but the basic process is to use some combination of
brew uninstall and
PostGIS Functions and Templates
PostGIS essentially takes the form of a couple of databast tables and a whole slew of database functions. You need to create a "template" database to store all this stuff, either for direct reference or for creating new databases.
If you aren't particularly familiar with running PostgreSQL from a terminal, you may need to start up the 9.0.x PostgreSQL server.
initdb /usr/local/var/postgres pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
If the first command barks at you about the folder already existing, it means you already had a version of PostgreSQL installed and it stuck its data in that folder. From here, you basically have 2 options. Either you can erase it (if your old PostgreSQL install didn't have important data) or you can put the PostgreSQL 9.0.x data somewhere else. If you put it somewhere else, you'll need to change the second command accordingly.
Install PostGIS Template Database
The basic idea, now, is to create a database that will serve as a template for future databases that will require PostGIS functions.
Assuming you've gotten PostGIS 1.5.x installed correctly using Homebrew, then you'll be able to run these commands:
createdb template_postgis psql -f /usr/local/share/postgis/postgis.sql template_postgis psql -f /usr/local/share/postgis/spatial_ref_sys.sql template_postgis
That will create a template database, containing some functions and a couple tables required to use PostGIS.
Create a New Database for your Django Project
Finally, instead of letting syncdb create your database, you'll want to create it manually using the template you just put together.
Again, from the terminal:
createdb -T template_postgis mydatabase
You should then be able to run syncdb against that database to generate your Django-based tables.
This was one of the more frustrating interactions I've had with Django/Python on OS X (probably second only to PIL, which oddly enough also installs beautifully using Homebrew).
If this didn't work for you, here's the resources I used to get me through it. I ended up cherry picking pieces from each of these to get things running on my machine. Maybe you'll see something I missed.
- Installing Django PostGIS Postgres on OS X: Version Hell
- Django and Postgis on OS X