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

/../Users/user/anaconda3/envs/virtual_environment/etc/conda/activate.d
/../Users/user/anaconda3/envs/virtual_environment/etc/conda/deactivate.d

In the activate.d directory create a file (if not exists) env_vars.sh 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) env_vars.sh with the content

unset HOST_NAME
unset DATABASE_NAME
unset DATABASE_USER
unset DATABASE_PASSWORD

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 base.py with the content

import os
from django.core.exceptions import ImproperlyConfigured

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

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

from . import base

Comment out the lines for the sqlite3

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

and add the following lines for the PostgreSQL

DATABASES = {
   '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 manage.py runserver

and on the browser type the link

http://127.0.0.1:8000/

You should now see the Django homepage.

Additional Django configuration
The initial migrations are made as

$ python manage.py makemigrations
$ python manage.py migrate

The Django superuser can be created as

$ python manage.py createsuperuser

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

$ python manage.py runserver

and on the browser type the link

http://127.0.0.1:8000/admin

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.