Install Django, GeoDjango, PostgreSQL & PostGIS on OSX Leopard

by tokumine

p230_02

This procedure will loosely follow the directions in the following locations:

I’ll assume you are using the terminal. You are also using OSX 10.5.6 (or later)

1) Install Django

Make a temporary directory to house the install files and enter that directory
mkdir django_install && cd django_install

Download the latest stable version of Django.
wget http://media.djangoproject.com/releases/1.1/Django-1.1-beta-1.tar.gz

Untar it into a temporary directory and cd into the directory
tar zxvf Django-1.1-beta-1.tar.gz && cd Django-1.1-beta-1

run the installer
sudo python setup.py install

These commands will install Django in your Python installation’s site-packages directory.

2) Install GeoDjango prerequisites

This section is from a web browser and the finder. It’s just easier that way.

Download the following drive image files, double click them to mount them as a drive, and run the mpkg installer inside each one. The order is important, so do them one by one:

These all setup your system with the right unix GIS libraries to support GeoDjango

3) Install OSX developer Tools

This bit is a total pain, but you only have to do it once. Break open the CD’s that came with your Mac, and find the one that says XCode or Developer tools or something.

Browse to the installer, and just install it all.

4) Install PostgreSQL & PostGIS

This section is from a web browser and the finder. It’s just easier that way.

Download the following drive image files, double click them to mount them as a drive, and run the mpkg installer inside each one. The order is important, so do them one by one:

5) Install pgAdmin3

This is the MSSQL enterprise manager of postgres. Download it and drag it to your applications folder. It won’t be able to connect to postgres yet, so don’t even both trying.

pgadmin3-1.8.4.dmg

6) Setup some paths

Back to the console now go to your home directory
cd $home

Add postgres binary path to your bash profile
echo "export PATH=$PATH:/usr/local/pgsql/bin" >> .bash_profile

Add a postgres data directory variable to your environment
echo "export PGDATA=/usr/local/pgsql/data" >> .bash_profile

quit the terminal and restart it

6) Install the Python PostgreSQL library

Back into the console now. Install the binding using the python easy_install command
sudo easy_install psycopg2

This may not work…

If for some reason, this install fails (and it failed on one box I installed), you’ll need to download the source, unpack and cd into it, edit setup.cfg with include dir and librarydir set to /usr/local/pgsql/include and /usr/local/pgsql/lib, and then run python setup.py build && python setup.py install

7) Create postgres user

Note, this may not be neccessary for some of you. I’d installed postgres before, and had somehow managed to mess up my box – hence the manual setup of postres user and rights. As Dane mentions in the comments below, this step shouldn’t really be needed. What can I say – I’m a linux hacker at heart, so permissions issues are always surmountable :). YMMV

Open system preferences > accounts and delete the postgres user.

click the + button and add a new user called postgreSQL, with a short name of “postgres”. Set the password to whatever you’d like. This is not necessary if the user already exists

8 ) Set permissions on postgres data directory

Go to your postgres data directory and sort permissions/ownership. Again, this may not be neccessary for you, but it was for me.

cd /usr/local/pgsql/
chmod -R 0700 data
chown -R postgres data

9) Create scripts to start and stop Postgres

For some reason, it’s not clear how to stop and start postgres from OSX, and using the kyngchaos binaries doesn’t seem to properly start them (or at least it didn’t on my machine). I think this was due to the permissions not being set properly on the data directory.

Go to your home directory
cd $home

create scripts to start/stop postgres && set permissions on them
echo "sudo launchctl load /Library/LaunchDaemons/org.postgresql.postgres.plist" >> start_pg.sh

echo "sudo launchctl unload /Library/LaunchDaemons/org.postgresql.postgres.plist" >> stop_pg.sh
chmod 755 start_pg.sh
chmod 755 stop_pg.sh

Finally, you should be able to start and stop postgres (check the activity monitor for postgres processes, making sure to view “all processes”) by running:

./start_pg.sh

and to stop:
./stop_pg.sh</code

from your home directory. If you can't see the postgres process in your activity monitor, let me know!)

9) initialise database

Ok, assuming we have postgres up and running and all the bindings sorted, we are ready to initialise our DB for our first project. At this point, I recommend following along from step 2 of the official geoDjango guide.

Make sure you read the code comments for the tutorial - in my case, the postgres shared directory was: /usr/local/pgsql/share, not /usr/share as in the docs.