How To Install Django With PostgreSQL In Ubuntu

how to install django with postgresql in ubuntu

How To Install Django With PostgreSQL In Ubuntu

 |   |  1
Django Programming Python

Django is an open source free software written in python. It allows less code to be used, and facilitates the creation of complex websites. It allows programmers avoid several other important security errors like the SQL Injection. You can quickly create Python web applications using Django, and rely on the framework to do a lot of heavy lifting.

With that being said, there is lot's of methods to install Django on Ubuntu or any linux distribution system. But I shares with you one of the easy method. The most flexible way to install Django on your system is within a virtual environment using virtualenv tool. After installation, you will start a new project to use as the basis for your site. Cheers!!


Nothing much more, Let's begin. Use some of your part of the brain and enter below comments in terminal. Use sudo if required, I don't used here because I running it on root user. Please note: It is better to use non-root user with sudo privileges before begins.

apt update
apt install python3-django python3-pip python3-dev libpq-dev
django-admin --version
pip3 install virtualenv 

Whenever you start a new project, you can create a virtual environment for it. Start by creating and moving into a new project directory (Here: folder named django under projects).

mkdir projects/django
cd projects/django
virtualenv insvenv
source insvenv/bin/activate
pip3 install django psycopg2 pipenv django-debug-toolbar

I used here DBMS as postgresql (popular choice for many small and large projects) and want to install only if not allready installed. It provides an implementation of the SQL querying language and having many advanced features like reliable transactions and concurrency without read locks.

apt install postgresql postgresql-contrib
sudo -i -u postgres
psql
psql -U postgres
ALTER USER postgres WITH PASSWORD 'postgres';
CREATEDB insproject
CREATE USER insprojectuser WITH PASSWORD 'insprojectuser';
ALTER ROLE insprojectuser SET client_encoding TO 'utf8';
ALTER ROLE insprojectuser SET default_transaction_isolation TO 'read committed';
ALTER ROLE insprojectuser SET timezone TO 'UTC';
GRANT ALL PRIVILEGES ON DATABASE insproject TO insprojectuser;
\q

Open pg_hba.conf and change as below (peer to md5).

subl /etc/postgresql/12/main/pg_hba.conf
# Database administrative login by Unix domain socket
local   all             postgres                                md5

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     md5
sudo service postgresql restart
django-admin startproject insproject .


Edit variables in projects/django/insproject/settings.py as below.

ALLOWED_HOSTS = ['127.0.0.1', 'localhost']
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'insproject',
        'USER': 'insprojectuser',
        'PASSWORD': 'insprojectuser',
        'HOST': 'localhost',
        'PORT': '',
    }
}

Then back to terminal and do comments below to create default tables and django superuser admin account.

./manage.py migrate
./manage.py createsuperuser
./manage.py runserver 0:7777

Visit your IP address followed by :7777 in your web browser like http://127.0.0.1:7777. You can see login form in http://127.0.0.1:7777/admin/and use the credentials that you entered when running createsuperuser comment.

Simple hacks to run server after setup

service postgresql restart && service apache2 restart && cd /home/projects/django && source insvenv/bin/activate && ./manage.py runserver 0:7777

Must installed packages

pip3 install django-debug-toolbar
pip3 install Pillow
pip3 install django_compressor

If you would like to share anything with me please comment below.




218 Claps

Show your love in the form of Claps and Comments...

Comments...

RIYAS - 

Very Helpful Thankyou