10

How can I connect to a different database when in a Django shell?

Something like:

python manage.py shell --database=slave

I tried googling all around, but couldn't find anything useful on this.

This is what my settings looks like:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'db1',
        'USER': 'user',
        'PASSWORD': 'password',
        'HOST': '10.10.10.10',
        'PORT': '',
        'CONN_MAX_AGE': 1000,
    },
    'slave':{
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'db2',
        'USER': 'user',
        'PASSWORD': 'password',
        'HOST': '10.10.10.10',
        'PORT': '',
        'CONN_MAX_AGE': 1000,
    },
}

3 Answers 3

11

You could select database in your query with using() ORM's method:

# This will hit a model in your default DB:
Model.objects.using('default').all()

# And this will hit a model in your slave DB:
Model.objects.using('slave').all()
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, this came up in my search, but I was planning to run a lot of queries on the slave, so was looking for a better way to do it. Another approach would have been to use a different settings file, and that seemed slightly better idea since I might want to run this on another slave later. Wanted to check if what I was looking for was directly supported. Anyways, thanks and +1.
10

You can split your settings module into submodules. For instance:

project/
    settings/
        __init__.py
        base.py
        dev.py
        prod.py
        shell.py

Your main/common settings are located in project/settings/base.py

In development, set DJANGO_SETTINGS_MODULE environment variable to project.settings.dev.

project/settings/dev.py may look like:

from .base import *

DEBUG = True

project/settings/shell.py may look like:

from .dev import *

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'db2',
        'USER': 'user',
        'PASSWORD': 'password',
        'HOST': '10.10.10.10',
        'PORT': '',
        'CONN_MAX_AGE': 1000,
    },
}

# OR
#
# DATABASES['master'] = DATABASES['default']
# DATABASES['default'] = DATABASES['slave']

Then run python manage.py shell --settings=project.settings.shell.


Or, alternatively, just create a second settings module:

project/
    settings.py
    shell_settings.py

project/shell_settings.py would look like:

from .settings import *

DATABASES['master'] = DATABASES['default']
DATABASES['default'] = DATABASES['slave']

and run python manage.py shell --settings=project.shell_settings

Comments

2

You can also use the connection module from django.db to create/connect to a brand new test database.

from django.db import connection
db = connection.creation.create_test_db()

I know it's not quite what was asked, but it's helpful and quick when testing. It will create a new test database and connect you to it. I haven't figured out how to connect to an existing database with it yet, but stay tuned.

1 Comment

You can use cursor = connection.cursor() to get a cursor to the default database.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.