Whenever working on a Django application, the first thing I ensure is to connect the application to a database. Databases are important for storing all kinds of data the application needs to deal with. Django is a powerful tool built using python that can connect to different types of databases including MySQL, PostgreSQL, Oracle DBMS, etc, and the interesting thing is that Django eases the overburden of writing database queries to create or delete tables via a concept known as ORM(Object Relational Mapping). Even Django offers a default sqlite3 database for small-scale projects or for development purposes which only needs to deal with a little amount of data. But for a larger application, we need to connect it to more featured and powerful databases and PostgreSQL is one of them. So in this article, we'll create a demo Django application and connect it to the PostgreSQL database.
Installing PostgreSQL and pgAdmin
pgAdmin is a tool for interacting with the PostgreSQL database and is easy to create, delete, manage databases without writing SQL queries. It is a web-based GUI interface tool used to connect and interact with the PostgreSQL database. You can install PostgreSQL and pgAdmin from these links:
Now let's open the PostgreSQL installer and it first asks for the directory to install, you can choose the installation directory or make it as default. Then you'll see a window for creating a password,
Remember the username which is postgres and create a password, click Next. The port number of PostgreSQL is 5432 keep it like that and install it.
Setting up PgAdmin
Open the PgAdmin installer you just downloaded and simply choose the path of installation, It is good to choose the same path where PostgreSQL is installed, But if you keep it default there is nothing to worry about. After installation, it'll launch the pgAdmin in your default browser.
In order to access the databases, you'll need to type the password configured before.
Creating a database
For creating a new database you can simply right-click on Databases then create.
Remember the password, username(Postgres), and database name for configuring in the Django application.
Creating a demo Django application
Here, I'm going to create a simple demo application so that you'll understand the whole process. First, we need to install Django, but hold on, when installing Django we don't want to disturb our entire system for the development purpose, So we'll use a virtual environment to store and run our Django application locally.
Installing virtual environment:
$ pip install virtualenvwrapper
Successfully installed pbr-5.8.1 stevedore-3.5.0 virtualenv-clone-0.5.7 virtualenvwrapper-4.8.4
$
$ mkvirtualenv test
$ workon test
(test) $
After the virtual environment is being installed, you can set up an environment using mkvirtualenv command and can activate it.
Installing Django:
(test) $ pip install django
Successfully installed django 4.0.2
Creating a Django project:
(test) $ django-admin startproject projects
(test) $ cd projects
(test) $ python manage.py runserver
The Django application will run on the localhost:8000
Projects folder:
Connecting to PostgreSQL
Now it's time to say hello to PostgreSQL, but hold on, Since Django and PostgreSQL are two different software how can we connect them together? For that, we need a connecter in between which is called psycopg2. You can install using pip.
Let's configure the settings in the Django projects folder in order to connect to PostgreSQL. Head over to the settings.py and search for Databases,
:
Change to:
:
After configuring the settings go to the terminal and run these commands:
python manage.py makemigrations
python manage.py migrate
The first command will create a table in the database and also create a migration file in the working directory. The migrate command will apply all the migrations to the database.
Creating a default superuser
For creating a superuser you need to run this command:
python manage.py createsuperuser
It will ask for creating a username, email, and password. After giving the details, the superuser is being created. Now run the localhost server again:
$ python manage.py runserver
You can log in to the superuser account from http://localhost:8000/admin.
If you're able to log in to the superuser account, it means the PostgreSQL is successfully connected to your Django application.
So our task is completed! But wait, how do we check the applied migrations in the database? Go to your pgAdmin dashboard and select your database, then you'll find schemas, under schemas there is an option called tables, if you expand the tables you can see all the tables created by migrations.