Setup of PostgreSQL with Django

Installation of PostgreSQL
Install PostgreSQL using the PostgreSQL Graphical Installer. The link to the installer can be found on the PostgreSQL homepage.

Creation of PostgreSQL database
Select a 'username' and 'password' for the new database (here we use a database name 'database_db'), and then type on the terminal window

$ sudo -u postgres createdb database_db

Provide the admin password for the PostgreSQL database. Then type

$ sudo -u postgres psql
postgres=# CREATE ROLE username WITH LOGIN PASSWORD 'password';
postgres=# GRANT ALL PRIVILEGES ON DATABASE database_db TO username;
postgres=# ALTER USER username CREATEDB;
postgres=# quit

Remember to include the semicolon ';' to the end of the commands.

Virtual environment for PostgreSQL
The base packages are already ready for the Django-Postgres setup. We define some parameters for the virtual environment. Navigate to the directory

$ cd /../Users/user/anaconda3/envs/virtual_environment

If not already exists create directories


In the activate.d directory create a file (if not exists) with the content

export HOST_NAME='localhost'
export DATABASE_NAME='database_db'
export DATABASE_USER='username'
export DATABASE_PASSWORD='password'

In the same way, in the deactivate.d directory create a file (if not exists) with the content


Restart (or close) the terminal server and activate the new virtual virtual_environment

$ source activate virtual_environment

Configuration of PostgreSQL in Django
In the Django directory ../Project/mysite/ create a file with the content

import os
from django.core.exceptions import ImproperlyConfigured

def get_env_variable(var_name):
      return os.environ[var_name]
   except KeyError:
      error_msg = "Set the %s environment variable"
      raise ImproperlyConfigured(error_msg)

Edit file ../Project/mysite/ and add in the beginning of the file the line

from . import base

Comment out the lines for the sqlite3

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

and add the following lines for the PostgreSQL

   'default': {
     'ENGINE': 'django.db.backends.postgresql_psycopg2',
     'NAME': base.get_env_variable('DATABASE_NAME'),
     'USER': base.get_env_variable('DATABASE_USER'),
     'PASSWORD': base.get_env_variable('DATABASE_PASSWORD'),
     'HOST': '',
     'PORT': '',

Now the Django-PostgreSQL setup should be ready to use. Type on the terminal window

$ python runserver

and on the browser type the link

You should now see the Django homepage.

Additional Django configuration
The initial migrations are made as

$ python makemigrations
$ python migrate

The Django superuser can be created as

$ python createsuperuser

After you have created the superuser, type on the terminal window

$ python runserver

and on the browser type the link

Now use your superuser credentials to login. Here you can, for example, create some additional user for a later purpose.

Return to the Mac main page.