1

I have 2 databases of interest, the basic one and the development one:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| projectsdb         |
| projectsdb_dev     |
+--------------------+
3 rows in set (0.00 sec)

In my django file mysite/mysite/settings.py, my databases are declared this way:

DATABASES = {  
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'projectsdb',
        'USER': 'projectsdb',
        'PASSWORD': 'notsecure',
        'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
    }
}

The allowed hosts is:

ALLOWED_HOSTS = ['xxx.xx.xxx.xx']  # I replaced it for the example

I start the server on the port 8006 which I use for developing:

$ python ./manage.py runserver xxx.xx.xxx.xx:8006

And here I modify the production database. I can switch to the dev database replacing the default database name:

DATABASES = {  
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'projectsdb_dev',
        'USER': 'projectsdb',
        'PASSWORD': 'notsecure',
        'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
    }
}

And it works fine, the server is interacting with the projectsdb_dev database. However I would like to keep both databases available in the settings file and I saw tutorials setting it up this way:

DATABASES = { 
    'default': {}, 
    'prod': {
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'projectsdb',
        'USER': 'projectsdb',
        'PASSWORD': 'notsecure',
        'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
    },  
    'dev': {
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'projectsdb_dev',
        'USER': 'projectsdb',
        'PASSWORD': 'notsecure',
        'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
    }   
}

Now when I open the web page on xxx.xx.xxx.xx:8006, I get this error:

ImproperlyConfigured at /admin/

settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.

I don't know if it's relevant but I also have this table:

mysql> select * from django_site;
+----+--------------------+----------------+
| id | domain             | name           |
+----+--------------------+----------------+
|  1 | example.com        | example.com    |
|  2 | xxx.xx.xxx.xx:8000 | projectsdb     |
|  3 | xxx.xx.xxx.xx:8006 | projectsdb_dev |
+----+--------------------+----------------+
3 rows in set (0.00 sec)

How can I run the server specifying the correct database I want?

2 Answers 2

6

I would say, create separate settings file for dev and prod. Or for the case of database only you can do with environment variables.

$ export ENV=PROD

Then in settings.py

import os
if os.environ.get('ENV') == "PROD":
    DATABASES = { 
      'default': {
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'projectsdb',
        'USER': 'projectsdb',
        'PASSWORD': 'notsecure',
        'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
        }, 
    }
else:
    DATABASES = { 
      'default': {
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'projectsdb_dev',
        'USER': 'projectsdb',
        'PASSWORD': 'notsecure',
        'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
        },
    } 
Sign up to request clarification or add additional context in comments.

2 Comments

How should the migrations for each database be managed? Can Django distinguish, between the created files in each migrations directory, the migrations that relate to each database?
Got the answer, it resides in the MIGRATION_MODULES setting which can also change based on the ENV variable.
1

When you don't specify a default database, you need to use automatic database routers in order to specify which database your models will use. You would write these router classes and then specify the DATABASE_ROUTERS setting to point to them. Routers basically contain some logic for telling database operations where to go, and you could include a check for whether or not this is a development environment.

Comments

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.