Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check which database is being used in a Django project

Tags:

python

django

In my Django project I want to use a queryset method that is only available in postgresql, if postgresql is being used.

How can I check the database from settings.DATABASES?

Assuming this structure:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2', # could be: 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'

My python skills are too weak to traverse that structure of dictionaries =(

like image 804
43Tesseracts Avatar asked Sep 28 '15 04:09

43Tesseracts


People also ask

How can I see my Django database?

If you're interested, run the command-line client for your database and type \dt (PostgreSQL), SHOW TABLES; (MariaDB, MySQL), .tables (SQLite), or SELECT TABLE_NAME FROM USER_TABLES; (Oracle) to display the tables Django created.

Which database is used in Django?

Django officially supports the following databases: PostgreSQL. MariaDB. MySQL.

Where is the database stored in Django?

sqlite3′ in your project directory. The file is a database file that will keep all of the data that you will be generating. Since Django is a server-side framework, it treats your computer as the host when you run the server from the command line or terminal.


2 Answers

This gives you the name of database backend configured as default in settings.DATABASES:

>>> from django.db import connection
>>> print connection.vendor
'sqlite'

In case you have multiple databases configured:

>>> from django.db import connections
>>> print connections['default'].vendor
'mysql'
>>> print connections['reporting'].vendor
'postgresql'
like image 114
Ozgur Vatansever Avatar answered Nov 14 '22 22:11

Ozgur Vatansever


from django.conf import settings

if settings.DATABASES['default']['ENGINE'] == 'django.db.backends.postgresql_psycopg2':
   # happy coding
like image 42
Geo Jacob Avatar answered Nov 14 '22 21:11

Geo Jacob