Complete Tutorial: Set-up PostgreSQL Database with Django Application
Set-up PostgreSQL database with Django application involves several steps. Here are detailed tutorials for each step:
Prerequisites:
- Python installed on your machine (preferably Python 3.x)
- PostgreSQL installed and running
Step 1. Create a Virtual Environment
python -m venv myenvActivate the virtual environment:
# On Windows:
venv\Scripts\activate
# On Unix or MacOS:
source venv/bin/activateStep 2: Setting Up the Environment
2.1 Install Django and PostgreSQL
First, make sure you have Python and pip installed. Then, install the Django and PostgreSQL adapters for Python:
pip install django psycopg2-binarypip install: Installs Python packages.django: Installs Django.psycopg2-binary: Installs the PostgreSQL adapter for Python.
Step 3: Create a Django Project
3.1 Create a Django Project
django-admin startproject myproject
cd myprojectdjango-admin startproject: Command to create a new Django project.myproject: Name of the project folder.
3.2 Create a Django App
python manage.py startapp myapppython manage.py startapp: Command to create a new Django app within the project.myapp: Name of the app.
Step 4: Configure PostgreSQL Database
4.1 Configure Database Settings
In myproject/settings.py, configure the database settings to use PostgreSQL:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'mydatabase',
'USER': 'myuser',
'PASSWORD': 'mypassword',
'HOST': 'localhost',
'PORT': '5432',
}
}DATABASES: Django settings for database configuration.ENGINE: Specifies the database engine (PostgreSQL in this case).NAME: Name of the database.USER: Username for accessing the database.PASSWORD: Password for the database user.HOST: Hostname where the database server is running.PORT: Port number on which the database server is listening.
Step 5: Create PostgreSQL Database
5.1 Access PostgreSQL Command Line
psql -U postgrespsql: Command-line interface for PostgreSQL.-U postgres: Login as the PostgreSQL superuserpostgres.
5.2 Create Database and User
CREATE DATABASE mydatabase;
CREATE USER myuser WITH PASSWORD 'mypassword';
ALTER ROLE myuser SET client_encoding TO 'utf8';
ALTER ROLE myuser SET default_transaction_isolation TO 'read committed';
ALTER ROLE myuser SET timezone TO 'UTC';
GRANT ALL PRIVILEGES ON DATABASE mydatabase TO myuser;CREATE DATABASE: Command to create a new database.CREATE USER: Command to create a new database user.ALTER ROLE: Commands to set client encoding, transaction isolation, and timezone for the user.GRANT ALL PRIVILEGES: Grant all privileges to the user on the created database.
Step 6: Migrate Database
python manage.py makemigrations
python manage.py migratepython manage.py makemigrations: Command to create migration files based on the changes in the models.python manage.py migrate: Command to apply migrations and create database tables.
Step 7: Create Superuser
python manage.py createsuperuserpython manage.py createsuperuser: Command to create a superuser to access the Django admin panel.
Step 8: Run Server
python manage.py runserverpython manage.py runserver: Command to start the Django development server.
Step 9: Verify Setup
Visit http://127.0.0.1:8000/admin/ and log in with the superuser credentials you created. If you can access the admin panel, your setup is successful.
Recap:
- Environment Setup: Install Django, PostgreSQL, and psycopg2-binary.
- Project Initialization: Create a Django project and app.
- Database Configuration: Configure PostgreSQL in
settings.py. - PostgreSQL Setup: Create a database and user in PostgreSQL.
- Migrations: Apply migrations to create necessary database tables.
- Superuser Creation: Create a superuser to access the Django admin panel.
- Run Server: Start the Django development server.
- Verification: Access the admin panel to ensure everything works.
This guide will get you up and running with a Django application using a PostgreSQL database.
Get Mehedi Khan’s stories in your inbox
Join Medium for free to get updates from this writer.
Thank you for reading. If you find something wrong or better ways to do it, let me know in the comments below.
If you like the post, hit the 👏 button below so that others may find it useful. You can follow me on:
GitHub | daily.dev | LinkedIn | YouTube










