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

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

Related

python/django database connection issue

Database settings
DATABASES = {
'default': {
'ENGINE': os.environ.get('DB_ENGINE', "mysql"),
'NAME': os.environ.get('DB_NAME', "django_db"),
'USER': os.environ.get('DB_USER', "root"),
'PASSWORD': os.environ.get('DB_PASS', "123456798"),
'HOST': os.environ.get('DB_HOST', "localhost"),
'PORT': os.environ.get('DB_PORT'),
}
}
error
django.core.exceptions.ImproperlyConfigured: 'mysql' isn't an available database backend or couldn't be imported. Check the above exception. To use one of the built-in backends, use 'django.db.backends.XXX', where XXX is one of:
'mysql', 'oracle', 'postgresql', 'sqlite3'
Connecting with mysql to generate migration but facing issue
Its my first time and facing following above issue please guide.
Instead of :
mysql in 'ENGINE': os.environ.get('DB_ENGINE', "mysql"),
Try this:
'ENGINE': os.environ.get('DB_ENGINE', "django.db.backends.mysql"),
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django_db',
'USER': 'root',
'PASSWORD': '123456798',
}
}
Try this to establish a connection.
I don't usually add HOST or Port but it still connect to database.
I only mention Host or Port if they don't have the default settings.

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.

django - Connect an existing MySQL db to Django

I have a database that i would like to use for an app in my new Django project.
I tried adding it to the setting.py by doing the following:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'Colta_create'),
'USER': 'root',
'PASSWORD': 'toor123',
'HOST': '',
'PORT': ''
}
}
I have installed mysqlclient, but i get this error:
django.db.utils.OperationalError: (1049, "Unknown database 'colta_create'")

Django - OperationalError: could not connect to server

Django Version : 1.5
Python Version : 2.7
python manage.py runserver 127.0.0.1:8000 command used to run the application and the following exception has been generated :
OperationalError: could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 8000?
The SETTINGS.py file for the Django project contains following database settings:
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'geodjango',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': '127.0.0.1',
'PORT': '8000',
}
}
You have invalid settings for DATABASES. Default port for postgres is 5432 (on some installations it is 5433):
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'geodjango',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}

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