How to set up Postgres in your Django project
how config postgres database for Django
Rinu Gour TDS Editors Django Stars
Hey, folks today we are learning How to set up Postgres in your Django project and find some interesting stuff.
Let’s dive in.
Prerequisites
In Django REST API’s common Python packages are required like
- Python
- Django
- Pgadmin4
- psycopg2-binary (poestgres connector package)
Setup
Here, We are going to setup how config postgres database for Django and install all required stuff .
here, I’m going to use virtualenv
for project environments ….
$ virtualenv venv
$ source venv/Scripts/activate
$ pip install django
$ django-admin startproject mymedbook .
$ python manage.py startapp accounts
In Above code snippets we are done setup project and created a project in root directory .
Here is file structure of corresponding Django project.
Now, we are going to major part of that project means set up Postgres in your Django project.
settings.py
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": "bhoomi",
"USER": "postgres",
"PASSWORD" : "gautamankul",
"HOST": "localhost",
"PORT": "5432",}
}
we have done, setup for postgres sql .
Note : In place of NAME
insert your DB name , and USER
will be your postgres username by default username of postgres sql is postgres
Migrations of DB
Next, step is applying migrations on ours DB in command shell
$ python manage.py makemigrations
if you got following error on your command shell don’t afraid …
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named 'psycopg2'
It’s saying you haven’t install postgres connector so install it following syntax
$ pip install psycopg2-binary
OR
$ pip install psycopg2
Now, applying migrations…………………………………………………………
$ python manage.py makemigrations
$ python manage.py migrate
Great if you like this article follow me and clapped it.