# Workshop 1. Installation

We will setup Django with PostgreSQL. Let's first install Django and then PostgreSQL.

# Task 1-1. Installing Django

Getting your Django in Ubuntu is a piece of cake. Just run a couple of commands and, boom!!, you have Django. But we will do this in a best practice manner, I think.

  1. Get your python3

You probably already have a python3 but just to be sure.

$ sudo apt update
$ sudo apt upgrade
$ sudo apt install build-essential python3 python3-pip 
1
2
3
  1. We will also get controlling the environment of the python3 with venv.
$ sudo apt install python3-venv
1
  1. Create a project folder.
$ cd ~/playGrd
$ mkdir myDjango-app
$ cd myDjango-app
1
2
3
  1. Create a virtual environment

python3 -m venv is a command executing the venv package. .venv is the name of the virtual environment. You can change the name of the environment but this name will be recognized by vscode.

$ python3 -m venv .venv
$ source .venv/bin/activate
(.venv) $
1
2
3
  1. Install Django
(.venv) $ pip3 install django
Collecting django
  Using cached Django-4.0-py3-none-any.whl (8.0 MB)
Collecting backports.zoneinfo; python_version < "3.9"
  Using cached backports.zoneinfo-0.2.1-cp38-cp38-manylinux1_x86_64.whl (74 kB)
Collecting sqlparse>=0.2.2
  Using cached sqlparse-0.4.2-py3-none-any.whl (42 kB)
Collecting asgiref<4,>=3.4.1
  Using cached asgiref-3.4.1-py3-none-any.whl (25 kB)
Installing collected packages: backports.zoneinfo, sqlparse, asgiref, django
Successfully installed asgiref-3.4.1 backports.zoneinfo-0.2.1 django-4.0 sqlparse-0.4.2
1
2
3
4
5
6
7
8
9
10
11
  1. Check that Django is installed
(.venv) $ python3 -m django --version
4.0
1
2

# Task 1-2. Installing PostgreSQL

Getting PostgreSQL is even easier in Ubuntu.

(.venv) $ sudo apt install postgresql
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libllvm10 libpq5 postgresql-12 postgresql-client-12 postgresql-client-common postgresql-common sysstat
Suggested packages:
  postgresql-doc postgresql-doc-12 libjson-perl isag
The following NEW packages will be installed:
  libllvm10 libpq5 postgresql postgresql-12 postgresql-client-12 postgresql-client-common postgresql-common sysstat
0 upgraded, 8 newly installed, 0 to remove and 0 not upgraded.
Need to get 30.6 MB of archives.
After this operation, 121 MB of additional disk space will be used.
Do you want to continue? [Y/n] 
1
2
3
4
5
6
7
8
9
10
11
12
13
14

Check that PostgreSQL is running fine.

(.venv) $ sudo service postgresql status
● postgresql.service - PostgreSQL RDBMS
     Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
     Active: active (exited) since Mon 2021-12-27 19:59:25 +07; 1min 16s ago
   Main PID: 57727 (code=exited, status=0/SUCCESS)
      Tasks: 0 (limit: 9430)
     Memory: 0B
     CGroup: /system.slice/postgresql.service

Dec 27 19:59:25 ubuntu systemd[1]: Starting PostgreSQL RDBMS...
Dec 27 19:59:25 ubuntu systemd[1]: Finished PostgreSQL RDBMS.
(.venv )$ sudo -u postgres psql -c "\conninfo"
You are connected to database "postgres" as user "postgres" via socket in "/var/run/postgresql" at port "5432".
1
2
3
4
5
6
7
8
9
10
11
12
13

By default, there is an account postgres created. We will assign a password for this account for use in the GUI.

(./venv) $ sudo -u postgres psql -c "\password postgres"
Enter new password: 
Enter it again: 
1
2
3

Now we install pgAdmin4 for accessing and managing our PostgreSQL. You can use any GUI you want. I like DBeaver because it has a beaver as a logo.

(.venv) $ sudo mkdir /var/lib/pgadmin
(.venv) $ sudo mkdir /var/log/pgadmin
(.venv) $ sudo chown $USER /var/lib/pgadmin
(.venv) $ sudo chown $USER /var/log/pgadmin
(.venv) $ pip3 install pgadmin4
.
.
.
(.venv) $ pgadmin4
NOTE: Configuring authentication for SERVER mode.

Enter the email address and password to use for the initial pgAdmin user account:

Email address: 
Password: 
Retype password:
Starting pgAdmin 4. Please navigate to http://127.0.0.1:5050 in your browser.
2021-12-28 00:01:59,000: WARNING	werkzeug:	WebSocket transport not available. Install simple-websocket for improved performance.
 * Serving Flask app 'pgadmin' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

Go to browser and go to http://127.0.0.1:5050. Login with pgAdmin account. At the dash board page, click Add Nre Server. In general tab, name your server. In Connection tab, fill in the following information.

  • Host name/address: localhost
  • Username: postgres
  • Password: {The password of postgres user you just change}

Click save.