2- How To Install Apache Django Postgresql on Ubuntu 22.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.

If virtual environment is activated, go ahead and deactivate that. 

Install Postgresql

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


Create a Database (movies_db) and Database User(movies_db_adm) for this project.  Change these according to your needs:

sudo -u postgres psql
CREATE USER movies_db_adm WITH PASSWORD 'YourPassword';
ALTER ROLE movies_db_adm SET client_encoding TO 'utf8';
ALTER ROLE movies_db_adm SET default_transaction_isolation TO 'read committed';
ALTER ROLE movies_db_adm SET timezone TO 'UTC';
GRANT ALL PRIVILEGES ON DATABASE movies_db TO movies_db_adm;
#Finally Quit:


Now make sure you activated the virtual environment

Install Postgresql database adapter.

cd /home/userfolder/Movies/
source v_env/bin/activate
pip install psycopg2


We need to edit our project's settings.py file for Postgresql database

nano PrjMovies/settings.py

Remove the existing DATABASES configuration and add your own config like below.

    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'movies_db',
        'USER': 'movies_db_adm',
        'PASSWORD': 'YourPassword',
        'HOST': 'prjmovies.localhost',
        'PORT': '',


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

sudo apt update
sudo apt upgrade

#pgAdmin 4 is available from Debian packages. 
#add pgAdmin 4 to our Ubuntu repository.
curl  -fsSL https://www.pgadmin.org/static/packages_pgadmin_org.pub | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/pgadmin.gpg

#Add repo to Debian packages
sudo sh -c 'echo "deb https://ftp.postgresql.org/pub/pgadmin/pgadmin4/apt/$(lsb_release -cs) pgadmin4 main" > /etc/apt/sources.list.d/pgadmin4.list'

#run system updates again and install pgadmin4
sudo apt update -y
sudo apt install pgadmin4


deactivate the virtual environment first 

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



You can now start PGAdmin on Ubuntu (Activities>PGAdmin).

It will ask you to set a master password. After you set the password.

Choose "Add a Server"> Under General Tab, enter a server name

Under Connection Tab> Enter Host Name or IP Address, postgres user password



(IMPORTANT: In your django app, you might want to add some additional fields to your user model such as gender, birthday etc. To do that you should extend the existing user model BEFORE runnin the commands below.  Otherwise extending the user model will be pain. (I always prefer extending the user model) If you DO NOT want to extend user model, you can run the migration commands and  create superuser and test your postgresql database. If you decided to extend the user model just skip the rest of this article. In the following articles, we will secure sensetive data, create a django app and then extend user model and finally we will run the commands below and complete the migrations)


python3 manage.py collectstatic
python3 manage.py makemigrations
python3 manage.py migrate
python3 manage.py createsuperuser

Go ahead and browse http://prjmovies.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. 

That's it. Now Apache Django and Postgresql are working altogether. Next article will be about securing our sensitive data which we keep in settings.py How To Install Apache Django Postgresql on Ubuntu 22.04 Part3