configure PostgreSQL in the Django application? Django is a web framework developed in the Python programming language. It is used to create web applications. By default, Django supports SQLite database configuration that is very easy for beginners to learn Django with the database. But, different RDBMS (relational database management systems) are used in software companies to create a project. PostgreSQL is the most widely used RDBMS(relational database management system). PostgreSQL database is open-source and free to use.
In this tutorial, we will learn how to configure PostgreSQL in the Django application? Here, I am going to explain each step to create a project and set up the database.
Prerequisite for configuring PostgreSQL in the Django
- Python must be installed in your system. Here I am using the 3.6 version
- PostgreSQL database must be installed in your system.
Steps to configure PostgreSQL in Django:
The database configuration part comes just after the installation of the project basic files that means Django framework basic files. So first, we will create a directory. Here, I am using Windows operating system. If you are using another operating system then some commands can be different.
Create a folder or directory. I am creating a directory with the name django_postgres
. One thing to keep in mind that always give folder names in small letters.
After that, open command prompt (Terminal) in that folder. To open command prompt with that folder path, just open the folder and right click with shift
button and click on open command windows here
Then, install virtual environment into your system by the following command.
pip install virtualenv
Now, create a virtual environment. The virtual environment is the separate space where your python project libraries will be installed. This is the better approach to create a Django application with a Virtual environment so that no other Django application will be impacted. To create a virtual environment, write the below command in the command prompt and hit enter.
python -m venv venv
Now, a folder with the name venv
will be created into your django_postgres
folder.
Now, activate the virtual environment so that all installed libraries and packages can be stored in a separate folder which means venv
folder. To activate the virtual environment, write the below command.
venv\Scripts\activate.bat
For python 3.9 users command will be venv\Scripts\activate
Now the screen will look like the below image.
Next, install Django by the below command.
pip install django
To connect with PostgreSQL, we need to install psycopg2
module. Install it by the below command.
pip install psycopg2
Now, let’s create a project named with myproject
. Create it by the below command.
django-admin startproject myproject
Now move the path to the next folder. Use this command cd myproject
and then run the below command to start the server.
python manage.py runserver
Now, open the given URL into the browser and check Django is properly working or not. If your screen is displaying the page like the below image then your Django application installed successfully.
Now, create a database into your PostgreSQL database. You can give the database name anything that you want. I am giving the name here django_postgres_db
Your project folder structure will look like the below image.
Open the setting.py
file and replace the Database code with the below code.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'django_postgres_db',
'USER': 'postgres',
'PASSWORD': '1234',
'HOST': 'localhost',
'PORT': '5432',
}
}
In the above code, the following keys represents the different values:
- NAME field represents the database name.
- User field represents the database username (my database username is postgres).
- PASSWORD field represents the database password (my database password is 1234).
- HOST represents the hostname. I am using localhost right now.
- PORT represents the port number. The default port number of the PostgreSQL database is 5432.
Now, save the file and go to the command prompt and run the below command one by one. If your server is running then stop the server first by using CTRL + C
command.
python manage.py makemigrations
python manage.py migrate
The first command makemigrations
will check that any model is changed or not and the second command migrate
will create or update the table fields into your database.
If your migrations run successfully then you can follow the next steps.
How to check database connected successfully?
Now, the time is to check that the database is connected successfully or not and it is working properly or not. Let’s create a user into the database by the below command. Write the below command into the command prompt.
python manage.py createsuperuser
Now, again run the below command to start the server.
python manage.py runserver
Open this URL into your browser http://127.0.0.1:8000/admin
Now, log in with your username and password that you had created. If the user is logged in successfully then your database working properly.
Read another tutorial: