Sarathlal N

Using PostgreSQL database with Django

In default, Django uses SQLite database & Django automatically creates a SQLite database for our project. But today, I like to use the PostgreSQL database in my application & the required steps are given below.

Connect to Postgres Database Server

sudo -u postgres psql

Creating Database

CREATE DATABASE mydb;

Creating User

CREATE USER myuser WITH ENCRYPTED PASSWORD 'mypass';

Modifying Connection Parameters for Django

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';

Granting Permission To The User

GRANT ALL PRIVILEGES ON DATABASE mydb TO myuser;

Exit the SQL prompt

\q

Update Django configuration

Open the settings.py file of our project and scroll to the database section. It looks like below one.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

Update these settings with our PostgreSQL database details.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'mydb',
        'USER': 'myuser',
        'PASSWORD': 'mypass',
        'HOST': 'localhost',
        'PORT': '',
    }
}

Verify the database connection

In the directory where manage.py script exists, run the below command.

python manage.py migrate

If there are no errors, database migration will happen. Else errors will be displayed.

Got a project in mind? Send me a quick message, and I'll get back to you within 24 hours!.

Recent Posts

  1. Disabling Payment Methods in WooCommerce Based on Conditions
  2. How to Update Product Quantity in WooCommerce Using Custom Code
  3. Dynamically Generating a Table of Contents in WordPress
  4. Direct Checkout in WooCommerce - Add Product to Cart from Checkout Page & Skip Shop, Product, and Cart Pages
  5. Understanding the Impact of git reset --hard Command

Your Questions / Comments

If you found this article interesting, found errors, or just want to discuss about it, please get in touch.