skip to Main Content

I am trying to run python tests using coverage. However, when I try to run the command, the following error appears:

$ python3 manage.py test
Creating test database for alias ‘default’…
/workspace/.pip-modules/lib/python3.8/site-packages/django/db/backends/postgresql/base.py:304: RuntimeWarning: Normally Django will use a connection to the ‘postgres’ database to avoid running initialization queries against the production database when it’s not needed (for example, when running tests). Django was unable to create a connection to the ‘postgres’ database and will use the first PostgreSQL database instead.
warnings.warn(
Got an error creating the test database: permission denied to create database

I am currently connect a PostgreSQL db from ElephantSQL. Project is mainy django3.2.18 and using python3.8.

I have check db connections and everything seems fine, I’m not sure what permission is needed.

I expected a htmlcov folder to be created so I can view what testing remains. And view this using the command python3 -m http.server

2

Answers


  1. The connection details for PostgreSQL normally includes: (database-)name, user, password, host and port.

    On ElephantSQL it seems like they create a database name for you from the start, with a name they automatically generate. For my account they have set the same database name as the username, which I assume is true for you as well.

    Judging by your error message it seems like you haven’t set any database name in your connection details in your code, which is why Django turns to the default database name which seems to be postgres.

    You don’t seem to have permission to create a new database (with another name) at ElephantSQL, which is usually because of safety reasons.

    So your only option is to update your django code to include the database name in the connection details you have been given by ElephantSQL.

    Login or Signup to reply.
  2. I am facing the same problem. Initially the problem was that I changed the access port of the postresql to be a different one from the standard one, since there were two databases running in parallel, so I needed another port for the second one. When I changed it back to the standard one, the problem sligthly changed, but the coverage still reports this error. I think that the DB is not ready as fast as the coverage would need it, so I assume that a "wait for the db" must be implemented somehow before actually running the tests, but I could not figure it how….

    Could it be coverage runs before the django bootstrapping (see here Django test coverage vs code coverage)? I don’t know…

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search