Django 2.0

How To Install Apache Django Postgresql on Ubuntu 16.04 Part2

In Previous Article, We have seen how we can run Django with Apache. Now we will install Postgres and configure it for our environment.

sudo apt-get update
sudo apt-get install libpq-dev postgresql postgresql-contrib

Create a Database and Database User:

sudo -u postgres psql
CREATE USER crm_db_adm WITH PASSWORD 'YourPassword';
ALTER ROLE crm_db_adm SET client_encoding TO 'utf8';
ALTER ROLE crm_db_adm SET default_transaction_isolation TO 'read committed';
ALTER ROLE crm_db_adm SET timezone TO 'UTC';
#Finally Quit:

Now make sure you activated the virtual environment

cd /home/djangoadm/CRM/
source v_env/bin/activate
pip install psycopg2

We need to edit our project’s file for Postgresql database

sudo nano PrjCrm/PrjCrm/
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'crm_db',
        'USER': 'crm_db_adm',
        'PASSWORD': 'YourPassword',
        'HOST': 'prjcrm.localhost',
        'PORT': '',

You may want to install PGAdmin (GUI Application for Postgresql)

sudo apt-get install pgadmin3

let’s change the default password for postgres user

sudo -u postgres psql
ALTER USER postgres  WITH PASSWORD 'YourPassword';

Restart Apache

sudo service apache2 restart

Change working directory to where resides

cd PrjCrm

Finally run the command below.

python3 collectstatic

(IMPORTANT: If you are planning to extend the existing user model, you should first extend the user model, then run migration commands below. Otherwise extending the user model will be a pain). Otherwise, you may now run the migration commands and create superuser and test your postgresql database.

python3 makemigrations
python3 migrate
python3 createsuperuser

Go ahead and browse http://prjcrm.localhost/admin . Create users or groups. Test your Django and Postgres on Apache. I hope everything will be fine. You can delete the db.sqlite3 file as well. It is in your Project’s folder (PrjCrm in my case)

That’s it. Now Apache Django and Postgresql are working altogether. I will add one more final article to Apache + Django + Psotgresql articles series about securing our sensitive data which we keep in How To Install Apache Django Postgresql on Ubuntu 18.04 Part3

Post Comment