Django Database connections

By default Django support sqlite3 database if developer want to change database like MySQL or any other we can only change it through setting.py file and run without and stuff.

Firstly, open the settings.py file of your web-application/ project and there find this part.

DATABASES = {

    'default': {

        'ENGINE': 'django.db.backends.sqlite3',

        'NAME': os.path.join(BASE_DIR, db.sqlite3'),

   }

}

DATABASES is a pre-defined dictionary in Django Framework with the ‘default’ as an index having the value for the main database where all the data is to be stored.

There can be multiple databases as we need data backups too but there is only one default database, although we won’t be adding multiple databases now. Yet we will add all database connections code here which is helpful for database connections:

MySQL connection code:

Install xampp server and configure using this

DATABASES = {  

    'default': {  

        'ENGINE': 'django.db.backends.mysql',  

        'NAME': 'dbName',  

        'USER':'root',  

        'PASSWORD':'mysql',  

        'HOST':'localhost',  

        'PORT':'3306'  

    }  

}

  

Postgresql connection code:

Download http://initd.org/psycopg/, then install it under Python PATH

>pip install psycopg2

and then configure using

DATABASES = { '

   default': {

      'ENGINE': 'django.db.backends.postgresql',

      'NAME': 'db_name', 'USER': 'db_user',

      'PASSWORD': 'db_user_password',

       'HOST': '',

       'PORT': 'db_port_number',

    }

}

Powered  by Codersarts