A R U N A R U N I S T O
Let me take you through a journey that
began with an intriguing interview test,
where the challenge was to weave the
powers of Django and PostgreSQL into a
harmonious web application.
The primary objective was to establish a
robust connection between Django, a high-
level Python web framework, and
PostgreSQL, an open-source relational
database management system.
In the midst of a challenging interview, I was
handed the task of building a web
application using Django and PostgreSQL as
the mandatory tech stack. The mission was
clear - create a connection that not only
functions but does so with finesse.
I used psycopg2 for postgresql integration.
psycopg2 is a PostgreSQL adapter for the
Python programming language. It is a
PostgreSQL database adapter that provides
a Python interface for interacting with
PostgreSQL databases. This library enables
Python applications to connect to, query,
and manage PostgreSQL databases seamle-
ssly.
Before using psycopg2, make sure to install
it using a package manager like pip
pip install psycopg2
Django Database Configuration: Configured
Django settings to connect to the
PostgreSQL database, specifying database
credentials and connection details.
For the configuration of postgres in django
goto settings.py file in your project folder
and find databases then change the config
settings like below shown
#change the settings.py for postgres
DATABASES = {
'default': {
"ENGINE": "django.db.backends.postgresql",
"NAME": "<database_name>"",
"USER": "<username>"",
"PASSWORD":"<password>"",
"HOST":"localhost",
"PORT":"5433",
},
'test': {
"ENGINE": "django.db.backends.postgresql",
"NAME": "<test_database_name>"",
"USER": "<username>"",
"PASSWORD":"<password>"",
"HOST":"localhost",
"PORT":"5433",
},
}
'default' and 'test': These are aliases for the
database configurations. 'default' is used for
your main application, while 'test' is
specifically for running tests.
'ENGINE': Specifies the database engine. In
this case, it's set to
"django.db.backends.postgresql", indicating
the use of the PostgreSQL database engine.
'NAME': The name of the database.
'USER': The username used to connect to the
PostgreSQL database.
'PASSWORD': The password for the
PostgreSQL user.
'HOST': The hostname or IP address of the
PostgreSQL server.
'PORT': The port number on which the
PostgreSQL server is running.
Make sure the PostgreSQL server is running,
and the specified database names exist.
Additionally, ensure that the PostgreSQL
server is configured to accept connections
on the specified host and port. Adjust the
settings accordingly if you are deploying the
application in a different environment, such
as a production server.