Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to setup Django/Postgresql on OS X Mountain Lion using Homebrew

Command Line Tools for Xcode

Command line tools comes bundle with Xcode prior to 4.3 version. After 4.3, you need to install a separated command line tools yourself.

First, go to this url and login using Apple Developer account (Free to register)

https://developer.apple.com/downloads/index.action

Look up the list and download the latest Command Line Tools for Xcode

Install Homebrew

Execute this command to install Homebrew

ruby <(curl -fsSkL raw.github.com/mxcl/homebrew/go)

Homebrew website http://mxcl.github.com/homebrew/

Install Python

brew install python

At this point, distribute and pip will also be installed by above command.

Add the following to .profile in your home folder

PATH=/usr/local/bin:/usr/local/share/python:$PATH
export PATH

Note: site-packages is in /usr/local/lib/python2.7/site-packages/

Install Virtualenv and Virtualenvwrapper

pip install virtualenv virtualenvwrapper
mkdir ~/.virtualenvs

Add the following to .profile in your home folder

export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
export PIP_VIRTUALENV_BASE=$WORKON_HOME

Create a new virtual environment by using

mkvirtualenv [your virtualenv name]

or create a new virtualenv without any existing libraries

mkvirtualenv --no-site-packages [your virtualenv name]

Then start working on virtual environment by

workon [your virtualenv name]

Install Postgresql

PYTHON=/usr/local/bin/python brew install postgresql
initdb -A trust /usr/local/var/postgres
initdb /usr/local/var/postgres -E utf8

Load Postgresql automatically when login (** DO NOT FORGET to change Postgresql version number below to match the current version of Postgres)

mkdir -p ~/Library/LaunchAgents
cp /usr/local/Cellar/postgresql/9.1.4/homebrew.mxcl.postgresql.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

Login to Postgresql using your Mac OSX login name

Note: pg_hba.conf is in /usr/local/var/postgres/

Useful commands

Start Postgresql Server manually
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start

Stop Postgresql Server manually
pg_ctl -D /usr/local/var/postgres stop -s -m fast

Install Psycopg2

Install this inside virtual environment to connect Django with Postgresql database server

pip install psycopg2

Install Django

pip install django

Resources

@TheNorthEestern

This comment has been minimized.

Copy link

@TheNorthEestern TheNorthEestern commented Aug 25, 2012

Thanks!

@chuender

This comment has been minimized.

Copy link

@chuender chuender commented Oct 8, 2012

When I run mkvirtualenv [your virtualenv name], I get -bash: mkvirtualenv: command not found. Please help. Thanks!

@sahaskatta

This comment has been minimized.

Copy link

@sahaskatta sahaskatta commented Oct 19, 2012

@chuender -- close your terminal and open it again. it should work.

@johnrees

This comment has been minimized.

Copy link

@johnrees johnrees commented Mar 22, 2013

@chuender You need to add python to your PATH, look at the last line when you type brew info python

@whopkins

This comment has been minimized.

Copy link

@whopkins whopkins commented Jun 30, 2013

@johnrees Sorry I'm a Mac newbie, and a little help would go a long way in this case! Thanks for addressing chuender's issue. I'm a bit confused on how to set python to my PATH. The last line of "brew info python" is a link to

https://github.com/mxcl/homebrew/wiki/Homebrew-and-Python

Seems like an informational page that brags a lot about how with Homebrew you don't need to set the PATH.

@mvollrath

This comment has been minimized.

Copy link

@mvollrath mvollrath commented Jul 17, 2013

My virtualenvwrapper.sh is located in /usr/local/bin/ instead of /usr/local/share/python/

@greyshi

This comment has been minimized.

Copy link

@greyshi greyshi commented Jul 27, 2013

mvollrath, I also struggled for a while before finding out that was the problem. I also had to do sudo pip install.

@koyoyo

This comment has been minimized.

Copy link

@koyoyo koyoyo commented Dec 3, 2013

Install Homebrew

ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go/install)"

Homebrew website http://brew.sh/

Install Virtualenv and Virtualenvwrapper

my virtualenvwrapper.sh is located in /usr/local/bin/

@ruangrit

This comment has been minimized.

Copy link

@ruangrit ruangrit commented Jun 27, 2014

thanks

@lisaq

This comment has been minimized.

Copy link

@lisaq lisaq commented Aug 3, 2014

Some suggestions/notes/whatever…

When I use virtualenv, the commands I use are different:
mkvirtualenv == virtualenv
workon == source envname/bin/activate

Also… "Login to Postgresql using your Mac OSX login name" - Presumably something is supposed to happen here, but I'm not sure what (I'm not seeing any sort of login, but I'm not sure what I should be looking for).

@andresarenasv

This comment has been minimized.

Copy link

@andresarenasv andresarenasv commented Mar 5, 2015

This is great! Thanks

@acwio

This comment has been minimized.

Copy link

@acwio acwio commented Jan 8, 2016

@lisaq Thanks so much. Worked wonders for me.

@yehiajbq

This comment has been minimized.

Copy link

@yehiajbq yehiajbq commented Jun 1, 2016

I got stuck on setting the path. I couldn't find the .profile in home folder to set the path name. Should i just ignore that step or?
Thank you.
Also I'm using OS X El Captain.

Yehia Qtaish
Software Engineering BS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment