kaligne
kaligne

Reputation: 3278

Django configuring different databases

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?

Upvotes: 1

Views: 3003

Answers (2)

N.D.C.
N.D.C.

Reputation: 1601

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.

Upvotes: 1

itzMEonTV
itzMEonTV

Reputation: 20369

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',
        },
    } 

Upvotes: 6

Related Questions