Django, adding multiple databases miscofigured my settings - python

I had a fully working test project which used only one database (Django 1.8 + PostgreSQL 9.1). Then, I was asked to add multiple databases. I dropped the one database I was using and created 3 new ones. I modified settings.py like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'core',
'USER': 'chris',
'PASSWORD': '***',
'HOST': 'localhost',
'PORT': '5432',
},
'db1': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'db1',
'USER': 'chris',
'PASSWORD': '***',
'HOST': 'localhost',
'PORT': '5432',
},
'db2': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'db2',
'USER': 'chris',
'PASSWORD': '***',
'HOST': 'localhost',
'PORT': '5432',
}
}
DEFAULT_INDEX_TABLESPACE = 'default'
(using automatic database routing and only one models.py)
When I try to make my first migrations I now get this:
django-admin.py makemigrations --database=db1
django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
But then I do this:
(testproject) chris#ubuntu-pc ~/D/w/testproject> python manage.py shell
>>> from django.conf import settings
>>> settings.DEFAULT_INDEX_TABLESPACE
'default'
>>> settings.configure()
RuntimeError: Settings already configured.
What am I doing wrong?

You should use manage.py inside the project instead of django-admin.py:
python manage.py makemigrations
You don't need to pass database names to makemigrations. But you do need to migrate each database separately while migrating.
python manage.py migrate --database=db1

Related

psycopg2 cant connect to postgres DB psycopg2.OperationalError

my db setting
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'test',
'HOST': '127.0.0.1',
'USER': 'postgres',
'PORT': '5432',
'PASSWORD': '1234'
}
}
i try python manage.py migrate but have this error
EDIT: it happen when i re-install python
I'm stupid, I wrote the wrong password in the settings
Change django.db.backends.postgresql_psycopg2 to django.db.backends.postgresql.

How to indicate to a specific version of database? [Django] [PostgreSQL]

I using django version 3.0.2.
I'd like to use postgreSQL as my instance db.
And there are two version postgreSQL in server.
After config setting.py DATABASES parameter, and runserver.
It showed error.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'dj_web_console',
'USER': 'django',
'PASSWORD': 'django',
'HOST': 'localhost',
'PORT': '',
}
}
psycopg2.OperationalError: FATAL: password authentication failed for user "django"
I'm sure that the username and password are correct.
How to config pg_path in Django as odoo:
In this case, I can use the specific version of pgsql. And run smoothly.
I solve this by indicating the port of the version I installed.
In this case, I have a version 9.6 which install in 5432 and the other is 5433.
So I just solved this by filling in 5433.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'dj_web_console',
'USER': 'django',
'PASSWORD': 'django',
'HOST': 'localhost',
'PORT': '5433',
}
}

How to connect mysql in django

Here is my connection details
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django4webo1',
'USER': 'root',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '3306',
}
}
when i start server error will come and i also need migrations table in db
super(Connection, self).__init__(*args, **kwargs2)
django.db.utils.OperationalError: (1049, "Unknown database 'django4webo1'")
You need to create first your mysql database and then configure your settings as:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '<your_db_name>',
'USER': '<your_username_in_db>'
'PASSWORD': '<your_password_to_access_db>',
'HOST': 'localhost',
'PORT': '3306'
}
}
Note: Before you run the mirations and migrate you need to create a mysql database, after that run the migrations python manage.py makemigrations and then migrate the database python manage.py migrate

How to use inspectdb for multiple mysql schemas in django?

This is the DATABASES dictionary in my Settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'account',
'USER': 'xxxx',
'PASSWORD': 'xxxx',
'HOST': 'localhost',
'PORT': '3306'
}
}
My database has multiple schemas, for example, account, login, notifications etc.
When I do a python manage.py inspectdb, I only get the classes for the tables in the "account" schema, but I want the classes for all the schemas.
I tried doing python manage.py inspectdb --database=login, but I get a django.db.utils.ConnectionDoesNotExist exception
But, if I change the settings to
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'account',
'USER': 'xxxx',
'PASSWORD': 'xxxx',
'HOST': 'localhost',
'PORT': '3306'
},
'login': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'login',
'USER': 'xxxx',
'PASSWORD': 'xxxx',
'HOST': 'localhost',
'PORT': '3306'
},
}
I get the proper classes of the table login on executing python manage.py inspectdb --database=login. But, problem is, if this is supposed to be the procedure, I have to make an option in the DATABASES option, which will become really long, as I have more than 15 schemas, and I am not sure if doing that will be a good thing or not.
So, I want to know what is the right way of doing this.
I am on Django 1.10.5.
Django needs to know how to reach each one of your databases so you won't be able to avoid declaring them in the DATABASES setting. If your only concern is that you will loose readability, you may create a helper function to get rid of repetition.
For example:
def local_db(name):
return {
'ENGINE': 'django.db.backends.mysql',
'NAME': name,
'USER': 'xxxx',
'PASSWORD': 'xxxx',
'HOST': 'localhost',
'PORT': '3306',
}
DATABASES = {
'default': local_db('account'),
'login': local_db('login'),
# Etc.
}
If you want to inspect all databases at once, you may create a custom command to do so.
For example:
from django.conf import settings
from django.core.management.commands.inspectdb import Command as BaseCommand
class Command(BaseCommand):
def handle(self, **options):
for db_name in settings.DATABASES:
options['database'] = db_name
self.stdout.write('# Generated while inspecting database %s\n' % db_name)
super().handle(**options)

Specify database to use in Django

I need to use multiple databases for my django project. The application works fine when there is only one database:
In setting.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mydb',
'USER': 'root',
'PASSWORD': '',
'HOST': '127.0.0.1',
'PORT': 3306,
},
But if I added more databases from the same engine:
DATABASES = {
'default':{},
'mydb1': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mydb1',
'USER': 'root',
'PASSWORD': '',
'HOST': '127.0.0.1',
'PORT': 3306,
},
'mydb2': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mydb2',
'USER': 'root',
'PASSWORD': '',
'HOST': '127.0.0.1',
'PORT': 3306,
}
}
it gives me following error:
ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.
Also, i tried:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mydb1',
'USER': 'root',
'PASSWORD': '',
'HOST': '127.0.0.1',
'PORT': 3306,
},
'mydb2': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mydb2',
'USER': 'root',
'PASSWORD': '',
'HOST': '127.0.0.1',
'PORT': 3306,
}
}
It only sees mydb1, not mydb2, when i tried query mydb2, it gives me:
DoesNotExist: Site matching query does not exist.
Do I need to define database route? it seems that I only need to do that for customized read/write.
Thanks
UPDATE:
In django docs, it says "The default routing scheme ensures that if a database isn't specified, all queries fall back to the default database".
So I guess my actual question is how do I specify a database to use for my queries?
It is explicetely stated in docs
The DATABASES setting must configure a default database; any number of
additional databases may also be specified.
If the concept of a default database doesn’t make sense in the context
of your project, you need to be careful to always specify the database
that you want to use.
As in your second example default database is not configured
DATABASES = {
'default':{},
...
}
when you access your data with no database specified, a django.db.backends.dummy backend is used, which complains on your configuration with ImproperlyConfigured error.
An example of configuring multiple database usage with Database Routers can be found in docs
update
Site matching query error is for completely different reasons, and is another question. Answer here, as it is duplicate of many others: as your mysql1 and mysql2 dbs have different content, second one seems to not to be properly configured. Refer site matching query does not exist.

Categories