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 DATABASE crm_db;
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';
GRANT ALL PRIVILEGES ON DATABASE crm_db TO crm_db_adm;
#Finally Quit:
\q

 

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 settings.py file for Postgresql database

sudo nano PrjCrm/PrjCrm/settings.py
DATABASES = {
    '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';
\q

 

Restart Apache

sudo service apache2 restart

 

Change working directory to where manage.py resides

cd PrjCrm

Finally run the command below.

python3 manage.py 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 manage.py makemigrations
python3 manage.py migrate
python3 manage.py 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 settings.py How To Install Apache Django Postgresql on Ubuntu 18.04 Part3

© selimatmaca.com. All Rights Reserved.