HisAbimbola     About     Archive     Feed

Steps to setup Postgres with Django on mac

Recently I started a Django project and for this project, I decided to use PostgreSQL inside of the default SQLite that comes with Django on a mac.

I ran into some issues that cost me a lot of billable hours, but I was finally able to fix it, hence this post so that nobody would waste lot of billable hours to setup PostgreSQL with Django.

There are some issues I encountered that may be peculiar to me, but also could be helpful to someone, so I would highlight that too.

Install Postgres


Django supports PostgreSQL 9.0 and higher.

In case you do not have Postgres on your system already, I would recommend you downloading and installing it from the package app on their site.

If you are like me that I forgot the password I used for Postgres on my machine, there are several ways to change your password, but I had nothing to lose so I just uninstalled the app. Double-click the uninstall-postgresql.app app in this directory

/Library/PostgreSQL/9.4/uninstall-postgresql.app/

That would do a total clean-up for you, then you can install Postgres from the app you downloaded.

Install Psycopg2


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

pip install psycopg2

Confirm that psycopy2 is working by running this command

python -c "import psycopg2"

If you get any error with that command, check out my post on psycopg2 error and how I solved it.

Install Django

You should install Django by running this command

pip install django

Create a database


Before Django can use Postgres it would need a database, you can create it from the GUI or run this command

CREATE DATABASE  OWNER
CREATE DATABASE

Set Postgres as your default database


The next thing to do is to update <projectname>/settings.py</projectname>

DATABASES = {

    "default": {

        "ENGINE": "django.db.backends.postgresql_psycopg2",

        "NAME": "<your database='' name=''>",

        "USER": "<the user=''>",

        "PASSWORD": "<your password=''>",

        "HOST": "localhost",

        "PORT": "5432",

    }

}

Run your migration


Run your migration to create tables in the database

python manage.py migrate

If your see something like this

Django Migration successful Then you have successfully setup Postgres with your Django app, if you encounter any challenges, please drop comments and let’s fix it then we update this post, to avoid others going through that also.

Great thanks to my colleagues Ifedapo and Eniola that helped me in solving this and other issues I had when setting up. Gracias

If you liked this post, you can share it with your followers or follow me on Twitter!