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.
  1. DATABASES = {
  2. 'default': {
  3. 'ENGINE': 'django.db.backends.mysql',
  4. 'NAME': 'dataflair',
  5. 'USER': 'root',
  6. 'PASSWORD': “”,
  7. 'HOST': “”,
  8. 'PORT': “”,
  9. 'OPTIONS': {
  10. 'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"
  11. }
  12. }

For the last steps just run these two commands:
  1. python manage.py migrate
  2. python manage.py runserver
 
 
 
 
 

 


Comments

Popular posts from this blog

ERR: error parsing query: found influx, expected SELECT, DELETE, SHOW, CREATE, DROP, EXPLAIN, GRANT, REVOKE, ALTER, SET, KILL at line

How To Convert Html File/URL/String Into Image Using Python

How to get Full URL or get absolute url In Django