makemigrations isn't working- how to fix it? - python

I have a dockerized django project. I'm trying to make migrations, but I keep getting this error. What does this mean, and how can I fix it? I am really new to this, so please be as detailed as possible! Thank you
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/Users/jasminedogu/opt/anaconda3/lib/python3.7/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/Users/jasminedogu/opt/anaconda3/lib/python3.7/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/jasminedogu/opt/anaconda3/lib/python3.7/site-packages/django/core/management/base.py", line 328, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/jasminedogu/opt/anaconda3/lib/python3.7/site-packages/django/core/management/base.py", line 369, in execute
output = self.handle(*args, **options)
File "/Users/jasminedogu/opt/anaconda3/lib/python3.7/site-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/Users/jasminedogu/opt/anaconda3/lib/python3.7/site-packages/django/core/management/commands/makemigrations.py", line 101, in handle
loader.check_consistent_history(connection)
File "/Users/jasminedogu/opt/anaconda3/lib/python3.7/site-packages/django/db/migrations/loader.py", line 283, in check_consistent_history
applied = recorder.applied_migrations()
File "/Users/jasminedogu/opt/anaconda3/lib/python3.7/site-packages/django/db/migrations/recorder.py", line 76, in applied_migrations
if self.has_table():
File "/Users/jasminedogu/opt/anaconda3/lib/python3.7/site-packages/django/db/migrations/recorder.py", line 56, in has_table
return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())
File "/Users/jasminedogu/opt/anaconda3/lib/python3.7/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/Users/jasminedogu/opt/anaconda3/lib/python3.7/site-packages/django/db/backends/base/base.py", line 260, in cursor
return self._cursor()
File "/Users/jasminedogu/opt/anaconda3/lib/python3.7/site-packages/django/db/backends/base/base.py", line 236, in _cursor
self.ensure_connection()
File "/Users/jasminedogu/opt/anaconda3/lib/python3.7/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/Users/jasminedogu/opt/anaconda3/lib/python3.7/site-packages/django/db/backends/base/base.py", line 220, in ensure_connection
self.connect()
File "/Users/jasminedogu/opt/anaconda3/lib/python3.7/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/Users/jasminedogu/opt/anaconda3/lib/python3.7/site-packages/django/db/backends/base/base.py", line 220, in ensure_connection
self.connect()
File "/Users/jasminedogu/opt/anaconda3/lib/python3.7/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/Users/jasminedogu/opt/anaconda3/lib/python3.7/site-packages/django/db/backends/base/base.py", line 197, in connect
self.connection = self.get_new_connection(conn_params)
File "/Users/jasminedogu/opt/anaconda3/lib/python3.7/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/Users/jasminedogu/opt/anaconda3/lib/python3.7/site-packages/django/db/backends/postgresql/base.py", line 185, in get_new_connection
connection = Database.connect(**conn_params)
File "/Users/jasminedogu/opt/anaconda3/lib/python3.7/site-packages/psycopg2/__init__.py", line 127, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

Your Django application is not connecting to the database.
Couple of things to check.
What port is your database running on and what is the hostname? If you're using docker-compose, the host will be the same as the database service name.
Next, check your settings.py insuring that your database is using the correct host and port. By default, on Postgres, Django will use port 5432. If this is not the case, update it.
Similarly, if you have not explicitly set a host, Django will use localhost. You will need to change this value to whatever your database service name is.

Related

Django: unknown database name

I am trying to change the database in django from sqlite3 to mysql
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django',
'USER':'myusername',
'PASSWORD':'mypassword',
'HOST':'127.0.0.1',
'PORT':'3306',
}
}
and after trying to run my server i get this error
System check identified no issues (0 silenced).
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\base\base.py", line 244, in ensure_connection
self.connect()
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\base\base.py", line 225, in connect
self.connection = self.get_new_connection(conn_params)
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\mysql\base.py", line 244, in get_new_connection
connection = Database.connect(**conn_params)
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\MySQLdb\__init__.py", line 123, in Connect
return Connection(*args, **kwargs)
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\MySQLdb\connections.py", line 185, in __init__
super().__init__(*args, **kwargs2)
MySQLdb.OperationalError: (1049, "Unknown database 'django'")
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\threading.py", line 973, in _bootstrap_inner
self.run()
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\threading.py", line 910, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\commands\runserver.py", line 137, in inner_run
self.check_migrations()
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\base.py", line 576, in check_migrations
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\migrations\executor.py", line 18, in __init__
self.loader = MigrationLoader(self.connection)
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\migrations\loader.py", line 58, in __init__
self.build_graph()
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\migrations\loader.py", line 235, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\migrations\recorder.py", line 81, in applied_migrations
if self.has_table():
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\migrations\recorder.py", line 57, in has_table
with self.connection.cursor() as cursor:
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\base\base.py", line 284, in cursor
return self._cursor()
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\base\base.py", line 260, in _cursor
self.ensure_connection()
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\base\base.py", line 244, in ensure_connection
self.connect()
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\utils.py", line 91, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\base\base.py", line 244, in ensure_connection
self.connect()
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\base\base.py", line 225, in connect
self.connection = self.get_new_connection(conn_params)
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\mysql\base.py", line 244, in get_new_connection
connection = Database.connect(**conn_params)
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\MySQLdb\__init__.py", line 123, in Connect
return Connection(*args, **kwargs)
File "C:\Users\ыв\AppData\Local\Programs\Python\Python39\lib\site-packages\MySQLdb\connections.py", line 185, in __init__
super().__init__(*args, **kwargs2)
django.db.utils.OperationalError: (1049, "Unknown database 'django'")
i dont know why django can't see my db, maybe you guys have some ideas.
Mysql didn't get me any problems before this case,
i haven't found a way to fix an error, so I counting on your help.
thanks in advance
The shortest answer to that is that django database ist't crated in MySQL. Django will not create the database for you like it do on sqlite3, you have to had this database created before.
You can ensure that you created the database by running a command like that:
$ mysql -h 127.0.0.1 -pmypassword -u myusername # to connect on your mysql
mysql> show databases; # list all databases on your MySQL server
To create a new database my recomendation is the following:
mysql> create database django charset utf8mb4; -- charset is optional
mysql> use django;

How can I solve application error on heroku?

I am trying to create an app on django and I am trying to host it on heroku. Everything runs perfectly locally. I get an error while trying to migrate the database onto the heroku app by running command -
heroku run python manage.py migrate
This is the error I am getting -
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/base.py", line 230, in ensure_connection
self.connect()
File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/asyncio.py", line 25, in inner
return func(*args, **kwargs)
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/base.py", line 211, in connect
self.connection = self.get_new_connection(conn_params)
File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/asyncio.py", line 25, in inner
return func(*args, **kwargs)
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 199, in get_new_connection
connection = Database.connect(**conn_params)
File "/app/.heroku/python/lib/python3.9/site-packages/psycopg2/__init__.py", line 121, in connect
dsn = _ext.make_dsn(dsn, **kwargs)
File "/app/.heroku/python/lib/python3.9/site-packages/psycopg2/extensions.py", line 167, in make_dsn
parse_dsn(dsn)
psycopg2.ProgrammingError: invalid dsn: invalid connection option "init_command"
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/app/manage.py", line 22, in <module>
main()
File "/app/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 425, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 419, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 373, in run_from_argv
self.execute(*args, **cmd_options)
File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 417, in execute
output = self.handle(*args, **options)
File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 90, in wrapped
res = handle_func(*args, **kwargs)
File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/commands/migrate.py", line 75, in handle
self.check(databases=[database])
File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 438, in check
all_issues = checks.run_checks(
File "/app/.heroku/python/lib/python3.9/site-packages/django/core/checks/registry.py", line 77, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "/app/.heroku/python/lib/python3.9/site-packages/django/core/checks/model_checks.py", line 34, in check_all_models
errors.extend(model.check(**kwargs))
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/base.py", line 1307, in check
*cls._check_indexes(databases),
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/base.py", line 1699, in _check_indexes
connection.features.supports_covering_indexes or
File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/postgresql/features.py", line 84, in is_postgresql_11
return self.connection.pg_version >= 110000
File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 339, in pg_version
with self.temporary_connection():
File "/app/.heroku/python/lib/python3.9/contextlib.py", line 119, in __enter__
return next(self.gen)
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/base.py", line 614, in temporary_connection
with self.cursor() as cursor:
File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/asyncio.py", line 25, in inner
return func(*args, **kwargs)
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/base.py", line 270, in cursor
return self._cursor()
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/base.py", line 246, in _cursor
self.ensure_connection()
File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/asyncio.py", line 25, in inner
return func(*args, **kwargs)
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/base.py", line 230, in ensure_connection
self.connect()
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/base.py", line 230, in ensure_connection
self.connect()
File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/asyncio.py", line 25, in inner
return func(*args, **kwargs)
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/base.py", line 211, in connect
self.connection = self.get_new_connection(conn_params)
File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/asyncio.py", line 25, in inner
return func(*args, **kwargs)
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 199, in get_new_connection
connection = Database.connect(**conn_params)
File "/app/.heroku/python/lib/python3.9/site-packages/psycopg2/__init__.py", line 121, in connect
dsn = _ext.make_dsn(dsn, **kwargs)
File "/app/.heroku/python/lib/python3.9/site-packages/psycopg2/extensions.py", line 167, in make_dsn
parse_dsn(dsn)
django.db.utils.ProgrammingError: invalid dsn: invalid connection option "init_command"
settings.py -
prod_db = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(prod_db)
settings.py ( database section ) -
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '****',
'USER': '****',
'PASSWORD': '****',
'HOST': '127.0.0.1',
'PORT': '3306',
'OPTIONS': {
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"
}
}
}
How can I solve this issue? Please ask if I need to add any of my files onto the question.
You are using MySQL for local development and PostgreSQL in production. The PostgreSQL connection is coming from the DATABASE_URL environment variable that dj_database_url uses by default.
Since you are updating your default database from the environment (not replacing it), the MySQL-specific init_command option is being left in your PostgreSQL configuration. The psycopg2 PostgreSQL driver doesn't know what to do with it.
A quick and dirty solution would be to completely your default database configuration instead of updating it:
DATABASES['default'] = prod_db
But I strongly urge you to use the same database server for development and production. As you have discovered, database engines aren't drop-in replacements for each other. In this case, it's likely easiest to switch from PostgreSQL to MySQL on Heroku.
First, let's get rid of the Postgres stuff:
Remove psycopg2 from your dependencies. Its presence likely caused Heroku to automatically provision a PostgreSQL database for you, which added to the confusion.
Optionally, de-provision the existing Postgres database. It's probably the free version so it isn't costing you anything, but it's a bit weird to have it hanging around if you are using a completely different database server.
Now, provision a MySQL server. I suggest you pick from the available addons. There are a few MySQL options, including JawsDB and ClearDB. Here is an example using ClearDB:
heroku addons:create cleardb:ignite
Ignite is the free tier for ClearDB. Once provisioned, your app will see a new environment variable CLEARDB_DATABASE_URL. Other addons will use other environment variable names, so make sure to read their documentation.
Finally, update your Django configuration. dj_database_url uses the DATABASE_URL environment variable by default, but we can tell it to use another:
prod_db = dj_database_url.config(env="CLEARDB_DATABASE_URL", conn_max_age=500)
DATABASES['default'].update(prod_db)

Migration error(django.db.utils.OperationalError)

I'm trying to migrate my custom user model and I run makemigrations command to make migrations for new models. But when I run migrate command it throws this error :
conn = _connect(dsn, connection_factory=connection_factory,
**kwasync) django.db.utils.OperationalError
Trace back:
(venv_ruling) C:\Users\enosh\venv_ruling\ruling>python manage.py migrate
Traceback (most recent call last):
File "C:\Users\enosh\venv_ruling\lib\site-packages\django\db\backends\base\base.py", line 219, in ensure_connection
self.connect()
File "C:\Users\enosh\venv_ruling\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:\Users\enosh\venv_ruling\lib\site-packages\django\db\backends\base\base.py", line 200, in connect
self.connection = self.get_new_connection(conn_params)
File "C:\Users\enosh\venv_ruling\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:\Users\enosh\venv_ruling\lib\site-packages\django\db\backends\postgresql\base.py", line 187, in get_new_connection
connection = Database.connect(**conn_params)
File "C:\Users\enosh\venv_ruling\lib\site-packages\psycopg2\__init__.py", line 127, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\enosh\venv_ruling\ruling\manage.py", line 22, in <module>
main()
File "C:\Users\enosh\venv_ruling\ruling\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\enosh\venv_ruling\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
utility.execute()
File "C:\Users\enosh\venv_ruling\lib\site-packages\django\core\management\__init__.py", line 413, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\enosh\venv_ruling\lib\site-packages\django\core\management\base.py", line 354, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\enosh\venv_ruling\lib\site-packages\django\core\management\base.py", line 398, in execute
output = self.handle(*args, **options)
File "C:\Users\enosh\venv_ruling\lib\site-packages\django\core\management\base.py", line 89, in wrapped
res = handle_func(*args, **kwargs)
File "C:\Users\enosh\venv_ruling\lib\site-packages\django\core\management\commands\migrate.py", line 75, in handle
self.check(databases=[database])
File "C:\Users\enosh\venv_ruling\lib\site-packages\django\core\management\base.py", line 419, in check
all_issues = checks.run_checks(
File "C:\Users\enosh\venv_ruling\lib\site-packages\django\core\checks\registry.py", line 76, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "C:\Users\enosh\venv_ruling\lib\site-packages\django\core\checks\model_checks.py", line 34, in check_all_models
errors.extend(model.check(**kwargs))
File "C:\Users\enosh\venv_ruling\lib\site-packages\django\db\models\base.py", line 1290, in check
*cls._check_indexes(databases),
File "C:\Users\enosh\venv_ruling\lib\site-packages\django\db\models\base.py", line 1680, in _check_indexes
connection.features.supports_covering_indexes or
File "C:\Users\enosh\venv_ruling\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\enosh\venv_ruling\lib\site-packages\django\db\backends\postgresql\features.py", line 93, in is_postgresql_11
return self.connection.pg_version >= 110000
File "C:\Users\enosh\venv_ruling\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\enosh\venv_ruling\lib\site-packages\django\db\backends\postgresql\base.py", line 329, in pg_version
with self.temporary_connection():
File "C:\Users\enosh\AppData\Local\Programs\Python\Python39\lib\contextlib.py", line 117, in __enter__
return next(self.gen)
File "C:\Users\enosh\venv_ruling\lib\site-packages\django\db\backends\base\base.py", line 603, in temporary_connection
with self.cursor() as cursor:
File "C:\Users\enosh\venv_ruling\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:\Users\enosh\venv_ruling\lib\site-packages\django\db\backends\base\base.py", line 259, in cursor
return self._cursor()
File "C:\Users\enosh\venv_ruling\lib\site-packages\django\db\backends\base\base.py", line 235, in _cursor
self.ensure_connection()
File "C:\Users\enosh\venv_ruling\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:\Users\enosh\venv_ruling\lib\site-packages\django\db\backends\base\base.py", line 219, in ensure_connection
self.connect()
File "C:\Users\enosh\venv_ruling\lib\site-packages\django\db\utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\enosh\venv_ruling\lib\site-packages\django\db\backends\base\base.py", line 219, in ensure_connection
self.connect()
File "C:\Users\enosh\venv_ruling\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:\Users\enosh\venv_ruling\lib\site-packages\django\db\backends\base\base.py", line 200, in connect
self.connection = self.get_new_connection(conn_params)
File "C:\Users\enosh\venv_ruling\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:\Users\enosh\venv_ruling\lib\site-packages\django\db\backends\postgresql\base.py", line 187, in get_new_connection
connection = Database.connect(**conn_params)
File "C:\Users\enosh\venv_ruling\lib\site-packages\psycopg2\__init__.py", line 127, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError
models.py
from django.contrib.auth.models import AbstractUser
class CustomUser(AbstractUser):
"""extend usermodel"""
class Meta:
verbose_name_plural = 'CustomUser'
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'rulings',
'USER': 'xxxxxxx',
'PASSWORD': 'xxxxxxx',
'HOST': '',
'PORT': '',
}
}
AUTH_USER_MODEL = 'accounts.CustomUser'
The postgresql's database is empty.(ver.12.6)
I just mentioned user model and settings in this question but still if more code is required then tell me I'll update my question with that information. Thank you
I don't know if you have left the port empty for the question or it is actually empty, but try this in the console and see if the connection is successful.
conn=psycopg2.connect(database="rulings", user="xxx", password="xxx",port=5432)
Also this might not be your port(it is the default but yours might be changed) check your port number \conninfo in the postgres shell. And let me know if it helps.
You have to check for the host_ip or the port number in the settings.py file of django.

What's the proper SSL parameter to pass into sqlalchemy's create_engine?

I'm having trouble figuring out the proper parameters for connecting to a Postgres database using Django on Heroku with Python 3.7.10 and sqlalchemy 1.3.23. I know that's not the latest release of sqlalchemy, but this started failing the other week when Python was updated from 3.7 so I'm puzzled as to what's going on. I'm finding sqlalchemy's documentation a little hard to navigate and it doesn't help that this stopped working when just Python was updated.
So basically in Python, the connection is being set in this line:
import records as records_client
DB_URL_FROM_ENV_VAR = 'postgresql+pg8000://user:password#host:5432/db?ssl=True'
db = records_client.Database(DB_URL_FROM_ENV_VAR)
And when I go to the implementation of Database, I get this:
class Database(object):
"""A Database connection."""
def __init__(self, db_url=None, **kwargs):
# If no db_url was provided, fallback to $DATABASE_URL.
self.db_url = db_url or DATABASE_URL
if not self.db_url:
raise ValueError('You must provide a db_url.')
self._engine = create_engine(self.db_url, **kwargs)
# Connect to the database.
self.db = self._engine.connect()
self.open = True
That appears to be a part of sqlalchemy.
The error I get when I run the script is: TypeError: connect() got an unexpected keyword argument 'ssl'.
I've tried changing the parameter to ssl_mode and also sslmode since I've seen SO posts and issues posted with similar questions, but neither of those changes have done anything. I've also tried adding another parameter to the Database call like so: db = records_client.Database(DB_URL_FROM_ENV_VAR, connect_args={'sslmode':'require'}) but all to no avail.
How do I configure the database URL and/or the connection attempt with SSL to connect to the database?
Here's a full stack trace with the following settings:
DB_URL_FROM_ENV_VAR = 'postgresql+pg8000://user:password#host:5432'
db = records_client.Database(DB_URL_FROM_ENV_VAR, connect_args={'sslmode':'require'})
Traceback (most recent call last):
File "appdir/manage.py", line 14, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\user\Github\django-app\venv-dev\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\Users\user\Github\django-app\venv-dev\lib\site-packages\django\core\management\__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\user\Github\django-app\venv-dev\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\user\Github\django-app\venv-dev\lib\site-packages\django\core\management\base.py", line 364, in execute
output = self.handle(*args, **options)
File "C:\Users\user\Github\django-app\appdir\apps\etl\management\commands\load.py", line 118, in handle
step.load(items=ds_items, period=period)
File "C:\Users\user\Github\django-app\appdir\apps\etl\models\job.py", line 89, in load
match_status = self.domain_source.load(*args, **kwargs)
File "C:\Users\user\Github\django-app\appdir\apps\etl\models\domain.py", line 90, in load
return self.load_profile.load(items=items, data=data, **kwargs)
File "C:\Users\user\Github\django-app\appdir\apps\etl\models\load_source.py", line 99, in load
db = records_client.Database(DB_URL_FROM_ENV_VAR, connect_args={'sslmode':'require'})
File "C:\Users\user\Github\django-app\venv-dev\lib\site-packages\records.py", line 245, in __init__
self.db = self._engine.connect()
File "C:\Users\user\Github\django-app\venv-dev\lib\site-packages\sqlalchemy\engine\base.py", line 2263, in connect
return self._connection_cls(self, **kwargs)
File "C:\Users\user\Github\django-app\venv-dev\lib\site-packages\sqlalchemy\engine\base.py", line 104, in __init__
else engine.raw_connection()
File "C:\Users\user\Github\django-app\venv-dev\lib\site-packages\sqlalchemy\engine\base.py", line 2370, in raw_connection
self.pool.unique_connection, _connection
File "C:\Users\user\Github\django-app\venv-dev\lib\site-packages\sqlalchemy\engine\base.py", line 2336, in _wrap_pool_connect
return fn()
File "C:\Users\user\Github\django-app\venv-dev\lib\site-packages\sqlalchemy\pool\base.py", line 304, in unique_connection
return _ConnectionFairy._checkout(self)
File "C:\Users\user\Github\django-app\venv-dev\lib\site-packages\sqlalchemy\pool\base.py", line 778, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "C:\Users\user\Github\django-app\venv-dev\lib\site-packages\sqlalchemy\pool\base.py", line 495, in checkout
rec = pool._do_get()
File "C:\Users\user\Github\django-app\venv-dev\lib\site-packages\sqlalchemy\pool\impl.py", line 140, in _do_get
self._dec_overflow()
File "C:\Users\user\Github\django-app\venv-dev\lib\site-packages\sqlalchemy\util\langhelpers.py", line 70, in __exit__
with_traceback=exc_tb,
File "C:\Users\user\Github\django-app\venv-dev\lib\site-packages\sqlalchemy\util\compat.py", line 182, in raise_
raise exception
File "C:\Users\user\Github\django-app\venv-dev\lib\site-packages\sqlalchemy\pool\impl.py", line 137, in _do_get
return self._create_connection()
File "C:\Users\user\Github\django-app\venv-dev\lib\site-packages\sqlalchemy\pool\base.py", line 309, in _create_connection
return _ConnectionRecord(self)
File "C:\Users\user\Github\django-app\venv-dev\lib\site-packages\sqlalchemy\pool\base.py", line 440, in __init__
self.__connect(first_connect_check=True)
File "C:\Users\user\Github\django-app\venv-dev\lib\site-packages\sqlalchemy\pool\base.py", line 661, in __connect
pool.logger.debug("Error on connect(): %s", e)
File "C:\Users\user\Github\django-app\venv-dev\lib\site-packages\sqlalchemy\util\langhelpers.py", line 70, in __exit__
with_traceback=exc_tb,
File "C:\Users\user\Github\django-app\venv-dev\lib\site-packages\sqlalchemy\util\compat.py", line 182, in raise_
raise exception
File "C:\Users\user\Github\django-app\venv-dev\lib\site-packages\sqlalchemy\pool\base.py", line 656, in __connect
connection = pool._invoke_creator(self)
File "C:\Users\user\Github\django-app\venv-dev\lib\site-packages\sqlalchemy\engine\strategies.py", line 114, in connect
return dialect.connect(*cargs, **cparams)
File "C:\Users\user\Github\django-app\venv-dev\lib\site-packages\sqlalchemy\engine\default.py", line 508, in connect
return self.dbapi.connect(*cargs, **cparams)
TypeError: connect() got an unexpected keyword argument 'sslmode'

Docker Instalation Kiwi Tcms with Postgresql

I started Kiwi TCMS in Docker (k8s) with Postgresql. It started ok, but when I wanted to run migrations it crashed. I used this docker-compose for inspiration.
Log shows problem with connection with MySQL. But I changed configuration for engine. See:
bash-4.2$ KIWI_DB_ENGINE=django.db.backends.postgresql_psycopg2 /Kiwi/manage.py migrate
django.db.backends.postgresql_psycopg2
Traceback (most recent call last):
File "/venv/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
self.connect()
File "/venv/lib/python3.6/site-packages/django/db/backends/base/base.py", line 194, in connect
self.connection = self.get_new_connection(conn_params)
File "/venv/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 227, in get_new_connection
return Database.connect(**conn_params)
File "/venv/lib/python3.6/site-packages/MySQLdb/__init__.py", line 85, in Connect
return Connection(*args, **kwargs)
File "/venv/lib/python3.6/site-packages/MySQLdb/connections.py", line 204, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2013, "Lost connection to MySQL server at 'reading initial communication packet', system error: 0")
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Kiwi/manage.py", line 13, in <module>
execute_from_command_line(sys.argv)
File "/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/venv/lib/python3.6/site-packages/django/core/management/base.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "/venv/lib/python3.6/site-packages/django/core/management/base.py", line 350, in execute
self.check()
File "/venv/lib/python3.6/site-packages/django/core/management/base.py", line 379, in check
include_deployment_checks=include_deployment_checks,
File "/venv/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 59, in _run_checks
issues = run_checks(tags=[Tags.database])
File "/venv/lib/python3.6/site-packages/django/core/checks/registry.py", line 71, in run_checks
new_errors = check(app_configs=app_configs)
File "/venv/lib/python3.6/site-packages/django/core/checks/database.py", line 10, in check_database_backends
issues.extend(conn.validation.check(**kwargs))
File "/venv/lib/python3.6/site-packages/django/db/backends/mysql/validation.py", line 9, in check
issues.extend(self._check_sql_mode(**kwargs))
File "/venv/lib/python3.6/site-packages/django/db/backends/mysql/validation.py", line 13, in _check_sql_mode
with self.connection.cursor() as cursor:
File "/venv/lib/python3.6/site-packages/django/db/backends/base/base.py", line 255, in cursor
return self._cursor()
File "/venv/lib/python3.6/site-packages/django/db/backends/base/base.py", line 232, in _cursor
self.ensure_connection()
File "/venv/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
self.connect()
File "/venv/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/venv/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
self.connect()
File "/venv/lib/python3.6/site-packages/django/db/backends/base/base.py", line 194, in connect
self.connection = self.get_new_connection(conn_params)
File "/venv/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 227, in get_new_connection
return Database.connect(**conn_params)
File "/venv/lib/python3.6/site-packages/MySQLdb/__init__.py", line 85, in Connect
return Connection(*args, **kwargs)
File "/venv/lib/python3.6/site-packages/MySQLdb/connections.py", line 204, in __init__
super(Connection, self).__init__(*args, **kwargs2)
django.db.utils.OperationalError: (2013, "Lost connection to MySQL server at 'reading initial communication packet', system error: 0")
Any ideas where can be problem please?
bash-4.2$ KIWI_DB_ENGINE=django.db.backends.postgresql_psycopg2 /Kiwi/manage.py migrate
django.db.backends.postgresql_psycopg2
This is setting an environment variable inside the docker container and I'm not sure if that works or which one takes precendece.
If you want somebody to help you post the entire docker-compose.yml (or whatever kubernetes control file you use). Clearly the application thinks it is configured to talk to MySQL and fails when MySQL isn't available.
So problem was with version of used docker image.
I used docker image tag kiwitcms/kiwi:6.2 and I thought it's the latest release. But it's not. This version(docker image) doesn't have code for changing DB engine. I changed version of docker tag to latest and it works pretty well now.

Categories