coddingtonbear/inthe.am

env for DB_NAME and POSTGRES_DB do not match

NGenetzky opened this issue · 0 comments

You will only observe this bug if you destroy the 'data-db' volume, which will cause the postgres container to attempt to create the database based on the environment variable below.

Here is stacktrace when error is recreated:

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 23, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()  File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 283, in run_from_argv    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.8/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 63, in execute
    return super(Command, self).execute(*args, **options)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 329, in execute
    self.check_migrations()
  File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 422, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File "/usr/local/lib/python3.8/site-packages/django/db/migrations/executor.py", line 20, in __init__
    self.loader = MigrationLoader(self.connection)  File "/usr/local/lib/python3.8/site-packages/django/db/migrations/loader.py", line 52, in __init__    self.build_graph()
  File "/usr/local/lib/python3.8/site-packages/django/db/migrations/loader.py", line 210, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "/usr/local/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 65, in applied_migrations
    self.ensure_schema()
  File "/usr/local/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 52, in ensure_schema    if self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()):
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 254, in cursor
    return self._cursor()  File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 229, in _cursor
    self.ensure_connection()  File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
    self.connect()
  File "/usr/local/lib/python3.8/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/local/lib/python3.8/site-packages/django/utils/six.py", line 685, in reraise    raise value.with_traceback(tb)
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
    self.connect()
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 189, in connect
    self.connection = self.get_new_connection(conn_params)  File "/usr/local/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 176, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/usr/local/lib/python3.8/site-packages/psycopg2/__init__.py", line 127, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: FATAL:  database "inthe_am" does not exist

Here is one potential solution that I could put into PR:

commit 19f9349786338740869246d930090b49657db42c
Author: Nathan Genetzky <nathan@genetzky.us>
Date:   Wed Dec 23 16:51:21 2020 -0600

    bugfix: env: POSTGRES_DB was not same as DB_NAME so initdb was wrong

diff --git a/.env b/.env
index a9c4371a..d6282159 100644
--- a/.env
+++ b/.env
@@ -5,7 +5,7 @@ DB_SERVICE=postgres
 DB_PORT=5432
 POSTGRES_USER=inthe_am_user
 POSTGRES_PASSWORD=InTheAm_PW
-POSTGRES_DB=inthe_am_db
+POSTGRES_DB=inthe_am
 LOG_DIR=/tmp/
 DOCKER=1
 TASK_STORAGE_PATH=/task_data