How to Start Django Project with a Database(PostgreSQL)

Prerequisites

  • Get familiar with Django to read or follow throughout this article understandably
  • If having no experience with Django beforehand, knowledge of Python programming is required

Software and Tools

Let’s get started !!

1. Create directory, and virtual environment

  • Create a project directory, in this case I use “django-postgres”
mkdir django-postgres
  • Point to django-postgres directory
cd django-postgres
  • Build virtual environment for the directory
python -m venv env
  • The last step is activating virtual environment, so you’ve to run the command below
env/scripts/activate
Complete this process (Virtual Environment) now

2. Install Django

pip install django
pip freeze
To see which pagekages are installed

3. Open a desired IDE or Text Editor

Open new window on VS Code, then click “Open Folder”
  • Click “Open folder” to import the directory we created beforehand into VisualStudio Code workspace
  • Then open the terminal, because it’s more convenient to run the code in the same terminal
env/scripts/activate
  • Hit enter, now we already activated virtual environment.

4. Start the Django project

django-admin startproject postgresTest
My Django project

5. Start Django app

  • In VisualStudio Code terminal, you’ve to point to the project directory using “cd” command
cd postgresTest
  • Run the following command to start your first app
python manage.py startapp testdb
Django app is created
  • Run the server to make sure that everything is okay
python manage.py runserver
No problem

6. Setting up a database server

  • Open pgAdmin4
  • Click server, and choose PostgreSQL version, in this article, I use version 9.6
Choose your PosgreSQL version
  • Then create our database name
Create a database
  • Define the database name, for me, I named “dbtest” for the main purpose of testing
Name the database you want
  • Now I finished creating the database. Of course in the table I have no tables yet.
dbtest (my database)

7. Get back to our code to config the database

  • Navigate to settings.py
  • Approximately, in line 76 of code, this is the database config part
. . .

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

. . .
  • NAME → Database name e.g. dbtest previously created in pgAdmin
  • USER → Database username (default is postgres)
  • PASSWORD → Database password
  • HOST → Database host (In development stage, use localhost or IP Address 127.0.0.1 also available)
  • PORT → The port that used to run the database (Default port is 5432)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'dbtest',
'USER': 'postgres',
'PASSWORD': '1234',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
settings.py

8. Create a table

  • Create a table by writing a class/object(ORM) to access the database instead of writing raw SQL (Django automatically converts Python class/object code to be raw SQL under the hood
from django.db import models
# Create your models here.class Teacher(models.Model): name = models.CharField(max_length=80) age = models.IntegerField()

9. Migrate the table to the PostgreSQL database

  • makemigrations → To update and see the history or transaction happened in our table (We have to run this command every time when something changes in models.py e.g. adding a new table, changing a field name, etc
  • migrate → The last step to submit or sent out our table into the database server
python manage.py makemigrations
If you didn’t register your app
  • Go to settings.py to register the app
Registrer app
No module named ‘psycog2’
pip install psycopg2
python manage.py makemigrations
successful makemigrations
python manage.py migrate
Successful migrate
  • Get back to pgAdmin
  • Double click on “Tables” in pgAdmin.
  • Now I’ve successfully created my PostgreSQL database and migrated all tables in my Django project to PostgreSQL
  • Right click → View/Edit Data → All Rows
teacher table

Conclusion

  • Creating virtual environment for our Django project
  • Creating Django project
  • Creating Django app
  • Running Django project
  • Playing around with pgAdmin
  • Writing a table using Django ORM without touching any SQL syntax
  • Creating and configuring our own database in PostgreSQL database
  • Setting up Django project with PostgreSQL
  • Learning about a database connector library for a specific database
  • Learning about migration process
  • Browsing our data in our database in pgAdmin

If this article is helpful, please clap to support me in continuing my good work, and do not hesitate to drop your comment below sharing me your opinion or recommendation.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store