title: Intermittent unit test failures in sqlAlchemy due to database not being dropped
body: <p>I have a collection of unit tests that run in a gitlab pipeline.  The following code is meant to rebuild the database between tests.  It works most of the time, but does not work every time, causing the pipeline to fail on some random test.</p>
  <pre><code>    @pytest.fixture
      def apply_migrations():
          with engine.connect() as conn:
              drop_database_connections(conn, cfg.POSTGRES_DB)
              conn.execute(f'DROP DATABASE IF EXISTS &quot;{cfg.POSTGRES_DB}&quot;')
              conn.execute(f'CREATE DATABASE &quot;{cfg.POSTGRES_DB}&quot; TEMPLATE {template_db_name}')
  </code></pre>
  <p>This code will create an error message like the following:</p>
  <pre><code>
  self = &lt;sqlalchemy.dialects.postgresql.psycopg2.PGDialect_psycopg2 object at 0x7f1762ec6f70&gt;
  cursor = &lt;cursor object at 0x7f175f654a90; closed: -1&gt;
  statement = 'CREATE DATABASE &quot;test&quot; TEMPLATE template_db_23381'
  parameters = {}
  context = &lt;sqlalchemy.dialects.postgresql.psycopg2.PGExecutionContext_psycopg2 object at 0x7f175f343eb0&gt;
      def do_execute(self, cursor, statement, parameters, context=None):
  &gt;       cursor.execute(statement, parameters)
  E       psycopg2.errors.UniqueViolation: duplicate key value violates unique constraint &quot;pg_database_datname_index&quot;
  E       DETAIL:  Key (datname)=(test) already exists.
  /usr/local/lib/python3.9/site-packages/sqlalchemy/engine/default.py:732: UniqueViolation
  The above exception was the direct cause of the following exception:
      @pytest.fixture
      def apply_migrations():
          with engine.connect() as conn:
              drop_database_connections(conn, cfg.POSTGRES_DB)
              conn.execute(f'DROP DATABASE IF EXISTS &quot;{cfg.POSTGRES_DB}&quot;')
  &gt;           conn.execute(f'CREATE DATABASE &quot;{cfg.POSTGRES_DB}&quot; TEMPLATE {template_db_name}')
  /app/services/api/tests/conftest.py:116:
  _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
  /usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py:1274: in execute
      return self._exec_driver_sql(
  /usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py:1578: in _exec_driver_sql
      ret = self._execute_context(
  /usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py:1845: in _execute_context
      self._handle_dbapi_exception(
  /usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py:2026: in _handle_dbapi_exception
      util.raise_(
  /usr/local/lib/python3.9/site-packages/sqlalchemy/util/compat.py:207: in raise_
      raise exception
  /usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py:1802: in _execute_context
      self.dialect.do_execute(
  _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
  self = &lt;sqlalchemy.dialects.postgresql.psycopg2.PGDialect_psycopg2 object at 0x7f1762ec6f70&gt;
  cursor = &lt;cursor object at 0x7f175f654a90; closed: -1&gt;
  statement = 'CREATE DATABASE &quot;test&quot; TEMPLATE template_db_23381'
  parameters = {}
  context = &lt;sqlalchemy.dialects.postgresql.psycopg2.PGExecutionContext_psycopg2 object at 0x7f175f343eb0&gt;
      def do_execute(self, cursor, statement, parameters, context=None):
  &gt;       cursor.execute(statement, parameters)
  E       sqlalchemy.exc.IntegrityError: (psycopg2.errors.UniqueViolation) duplicate key value violates unique constraint &quot;pg_database_datname_index&quot;
  E       DETAIL:  Key (datname)=(test) already exists.
  E       
  E       [SQL: CREATE DATABASE &quot;test&quot; TEMPLATE template_db_23381]
  E       (Background on this error at: https://sqlalche.me/e/14/gkpj)
  /usr/local/lib/python3.9/site-packages/sqlalchemy/engine/default.py:732: IntegrityError 
  </code></pre>
  <p>Why does this database still exist sometimes after being dropped?  Using postgres 12.7 and sqlalchemy 1.4.31.</p>
  <p>I've tried to research this and found very little information.</p>