How to use PostgreSQL with Django on OS X

In this tutorial I’ll describe how to use PostgreSQL with Django running on OS X.

First, install PostgreSQL. You can do this using several methods. For this tutorial we use brew by running the following command in a terminal:

    brew install postgresql

Next, start the PostgreSQL server by running the following command:

    postgres -D /usr/local/var/postgres

Note: For information on how to make PostgreSQL start automatically run the command:

    brew info postgres

Now we’ve started postgress, we can create a database called myproject by running the following command:

    createdb myproject

Next, log into the Postgres session in the terminal by running

    psql myproject

Then create a user for the project using the command:

    CREATE USER myprojectuser WITH PASSWORD 'password';

Next, set encoding, transaction isoloation (to block reads from uncommitted transactions) and timezone using the following commands:

    ALTER ROLE myprojectuser SET client_encoding TO 'utf8';
    ALTER ROLE myprojectuser SET default_transaction_isolation \
 TO 'read committed';
    ALTER ROLE myprojectuser SET timezone TO 'UTC';

Next, grant the db user privileges with the command:

    GRANT ALL PRIVILEGES ON DATABASE myproject TO myprojectuser;

Exit the SQL session using the command:

    Ctrl D

Now we are going to install Django. I assume you have virtualenvwrapper installed.

Create your new virtual environment using the command:

    mkvirtualenv myprojectenv

This creates and switches to the new environment.

In here we then install Django and Psycopg2 using the commands:

    pip install django psycopg2

Next, create a folder at a suitable location for your project using the commands:

    mkdir ~/myproject
    cd $_

Then create a Django project in the created folder using the command:

    django-admin.py startproject myproject .

Next, we need to tell Django to use Postgres since it uses SQlite by default. Open myproject/settings.py with your favourite editor.

Find the lines:

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        }
    }

And change it to

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'NAME': 'myproject',
            'USER': 'myprojectuser',
            'PASSWORD': 'password',
            'HOST': 'localhost',
            'PORT': '',
        }
    }

Now we shall migrate the database using the following commands:

    python manage.py makemigrations
    python manage.py migrate

Note: On running the first command you may see an output saying No changes were detected. Ignore it.

You may now create a superuser using the command:

python manage.py createsuperuser

Now that is done, you may start your server using the command:

    python manage.py runserver 0.0.0.0:8000

That’s it! You can now view your site by visiting http://localhost:8000 in your browser. You should see the default index page. Using the admin credentials you created earler, you should be able to log in to the admin at http://localhost:8000/admin

Sources

How To Use PostgreSQL with your Django Application on Ubuntu 14.04 | DigitalOcean. https://www.digitalocean.com/community/tutorials/how-to-use-postgresql-with-your-django-application-on-ubuntu-14-04

PostgreSQL: Documentation: 9.4: PostgreSQL 9.4.5 Documentation. http://www.postgresql.org/docs/9.4/static/index.html