In our latest project we are using the Django Framework and we decided to use Postgres as the backend. Following are the steps

Change the DATABASES variable in the file.

         default': {
             'ENGINE': 'django.db.backends.postgresql_psycopg2',
             'NAME': 'postgres',
             'USER': 'postgres',
             'HOST': 'localhost',
             'PORT': '5432',

The settings will make sense as we continue with the steps.

The next step would be to download the PostgresApp

To enable Django to connect to the Postgres DB we have to make sure that psycopg2 is installed. psycopg2 provides a Python DB API which a python framework like Django can use. The easiest way to install psycopg2 is using pip. Just do pip install psycopg2 and we are done. Or are we?

One of the most common error that is thrown while installing psycopg2 ( Mac ) is

Error: pg_config executable not found.

Please add the directory containing pg_config to the PATH

or specify the full executable path with the option:

    python build_ext --pg-config /path/to/pg_config build ...

or with the pg_config option in 'setup.cfg'.

This is easily fixable. All one has to do is

export PATH=/Applications/<Version>/bin:$PATH

We are just adding the directory containing pg_config to the system path as the error instructed us to do. Running pip install psycopg2 should install it now. The next step is to get the postgres server started on our machine and create the tables required for the models in our app.

When we start the Postgres app a postgres server is started at port 5432. This is the reason why we have the HOST as localhost and port as 5432 in our settings file.

Next in the terminal for our postgres server we type the command


which creates a database named postgres with a superuser also named postgres (as mentioned in the settings file).

We are almost ready. The last step is to create migrations and migrate them

python makemigrations
python migrate

Our Postgres database is setup and ready to use.

Check out some apps that I built :