Skip to content

Instantly share code, notes, and snippets.

@panuta
Last active April 8, 2020 16:46
Show Gist options
  • Star 69 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • 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

@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