Django-03: Connect PostgreSQL with Django

Lokesh sharma
Nov 3 · 3 min read

In this guide, we’ll demonstrate how to install and configure PostgreSQL to use with your Django applications.

  1. Install the Components from the Ubuntu Repositories
$ sudo apt-get update
$ sudo apt-get install python-pip python-dev libpq-dev postgresql postgresql-contrib

2. Create a Database and Database User

$ sudo su - postgres

You should now be in a shell session for the postgres user. Log into a Postgres session by typing:

$ psql

First, we will create a database for our Django project. Each project should have its own isolated database for security reasons. We will call our database myproject in this guide, but it’s always better to select something more descriptive:

postgres=# CREATE DATABASE myproject;

Next, we will create a database user which we will use to connect to and interact with the database.

postgres=# CREATE USER myprojectuser WITH PASSWORD 'password';

Afterwards, we’ll modify a few of the connection parameters for the user we just created.

postgres=# ALTER ROLE myprojectuser SET client_encoding TO 'utf8';
postgres=# ALTER ROLE myprojectuser SET default_transaction_isolation TO 'read committed';
postgres=# ALTER ROLE myprojectuser SET timezone TO 'UTC';

Now, all we need to do is give our database user access rights to the database we created:

postgres=# GRANT ALL PRIVILEGES ON DATABASE myproject TO myprojectuser;

Exit the SQL prompt to get back to the postgres user’s shell session:

postgres=# \q

Exit out of the postgres user’s shell session to get back to your regular user’s shell session:

$ exit

3. Install Django and Create Model:

Now that our database is set up, we can install Django and create model in it.

Follow steps from my another Post:

1. For Setup: Installing Django on Ubuntu

2. For Model Creation: Create Model

4. Install psycopg2 in Virtual Environment :

Psycopg is the most popular PostgreSQL adapter for the Python programming language. At its core it fully implements the Python DB API 2.0 specifications. Several extensions allow access to many of the features offered by PostgreSQL.

$ pip install psycopg2

6. Configure the Django Database Settings:

Open the main Django project settings file located within the child project directory:

nano ~/myproject/myproject/settings.py

Towards the bottom of the file, you will see a DATABASES section that looks like this:

. . .DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
. . .

This is currently configured to use SQLite as a database. We need to change this so that our PostgreSQL database is used instead.

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'myproject',
'USER': 'myprojectuser',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '',
}
}

When you are finished, save and close the file.

7. Migrate the Database and Test your Project:

Now that the Django settings are configured, we can migrate our data structures to our database and test out.

cd ~/myproject
$ python manage.py makemigrations
$ python manage.py migrate

8. Opening admin Panel and View Our created Table:

Once you have an admin account set up, you can test that your database is performing correctly by starting up the Django development server.

$ python manage.py runserver

Open Browser and open url http://127.0.0.1:8000/admin

it will open admin login page.login with your superuser credentials.

9. Verifying Table:

after login you will see Post entry is available there.click on this and insert data in table from here.

Conclusion

In this guide, we’ve demonstrated how to install and configure PostgreSQL as the backend database for a Django project.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade