Skip to content

Instantly share code, notes, and snippets.

@panuta
Last active June 16, 2024 18:15
Show Gist options
  • Save panuta/1852087 to your computer and use it in GitHub Desktop.
Save panuta/1852087 to your computer and use it in GitHub Desktop.
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

@sahaskatta
Copy link

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

@johnrees
Copy link

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

@whopkins
Copy link

@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
Copy link

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

@greyshi
Copy link

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
Copy link

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
Copy link

thanks

@lisaq
Copy link

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
Copy link

This is great! Thanks

@acwio
Copy link

acwio commented Jan 8, 2016

@lisaq Thanks so much. Worked wonders for me.

@yehiajbq
Copy link

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