How to Connect Django With PostgreSql / Mysql / Oracel Database
How to Connect Django With PostgreSql Database:
Before need to Download: PostgreSql (download) and PG Admin- Download ( To view Data We use This Software)
Install psycopg2 Package in your virtual environment or Command Prompt ( Using pip install django psycopg2)
Goto your settings.py in your django project and add
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': 'password,
'HOST': 'localhost',
'PORT': '5432',
}
}
cd ~/myproject
python manage.py makemigrations
python manage.py migrate
After creating the database structure, we can create an administrative account by typing:
python manage.py createsuperuser
How to Connect Django With Mysql Database:
Here are the steps to integrate Django project with MySQL:
i. Install Xampp
Xampp is a free opensource tool which provides you with the Apache server and phpMyAdmin which is the best source for beginner programmers to work with MySQL.
You can download the Xampp according to your system from here:
https://www.apachefriends.org/download.html
2. Run Xampp Control Panel
Note:
Start the Apache Server First and then the MySQL server.
Now, click on the Admin of the MySQL Service, that should open a webpage(offline) looking like this.
iii. Creating a SQL database:
Just Click on the New button as shown here. Then, just fill the desired name of your database and click on create button.That’s it, now we don’t need to do anything here. We will be only interacting with python and the models component of Django will prepare everything for us
pip install mysqlclient (command Promt)
iv. Modifying settings.py
After that exchange this code with the DATABASE dictionary in settings.py.
- DATABASES = {
- 'default': {
- 'ENGINE': 'django.db.backends.mysql',
- 'NAME': 'dataflair',
- 'USER': 'root',
- 'PASSWORD': “”,
- 'HOST': “”,
- 'PORT': “”,
- 'OPTIONS': {
- 'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"
- }
- }
For the last steps just run these two commands:
- python manage.py migrate
- python manage.py runserver
Comments
Post a Comment