psycopg2 cant connect to postgres DB psycopg2.OperationalError - python

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.

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.

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

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'")

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)

Categories