How To Configure Database For Django


By default, the configuration uses SQLite.If you’re just interested in trying Django, this is the easiest choice. SQLite is included in Python, so you won’t need to install anything else to support your database. I would suggest you to work with this and dont get distracted with PostgreSQL setup while learning django. Your focus should be on learning django and not database setup. Besides, django works excatly same (*meaning, you dont need to change your business logic if you change database*) with SQLite or PostgreSQL. ## Default configuration ```python DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': BASE_DIR / 'db.sqlite3', } } ``` ## Using PostgreSQL When you want to start using PostgreSQL, please do following changes in your `settings.py` file. Before you make these changes, please ensure, database and users are created. ```python DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'mydatabase', 'USER': 'myuser', 'PASSWORD': 'mypassword', 'HOST': 'localhost', #If you arer using remote database, please use database server name 'PORT': '5432', # this is default port for PostgreSQL, you can change it based on actual port. } } ``` I am adding sample query for creating database and user to be used above ```bash CREATE DATABASE mydatabase DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci; CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword'; GRANT ALL ON mydatabase.* TO 'myuser'@'localhost'; FLUSH PRIVILEGES; exit; ```

This post is written by Pravin

Tags : Django

Share it :      

Leave a comment

captcha


Tags

Elsewhere

  1. GitHub
Ad below