14

My unittests are extending from django.test.TestCase. Using an SQLite3 backend, they are running fine.

I want to run them with a PostgreSQL backend, and the first thing that the tests are doing is to try and create a test database. This fails, since django has no permissions to do this. I want to configure PostgreSQL / django so that this operation is allowed, but I can not find any documentation on how to do this.

These are the settings used to connect to the database (settings.py):

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'mydb',
        'USER': 'myuser',
        'PASSWORD': 'myppasss',
        'HOST': 'localhost',
        'PORT': '',
    }
}

When running the django application, I have always a pre-created database and user, so that django can connect.

I have two questions:

  • What are the connection settings used to create the database, during unit test? My understanding is that the user that django is using to connect to the application database does not need to (and should not) have database creation rights.
  • Where is all this described? The closest I have found is here (TEST key in the DATABASES config parameter), but those settings mostly refer to Oracle databases, and have a different purpose.
blueFast
  • 41,341
  • 63
  • 198
  • 344
  • Django will use the [same connection settings as in your settings.py](https://docs.djangoproject.com/en/1.8/topics/testing/overview/#the-test-database) for tests, but will use a different database (by default `test_mydb`). In my answer to [this question](http://stackoverflow.com/questions/14186055/django-test-app-error-got-an-error-creating-the-test-database-permission-deni/14186439#14186439), I gave instructions for how to add the createdb permission to a user. I don't know whether it's possible to restrict the permission so that the django user can only create the database `test_mydb`. – Alasdair Oct 20 '15 at 09:33
  • @Alasdair: This is a bit surprising for me but it works indeed. If you care to reply, I will accept your answer. – blueFast Oct 20 '15 at 09:56
  • Possible duplicate of [django test app error - Got an error creating the test database: permission denied to create database](https://stackoverflow.com/questions/14186055/django-test-app-error-got-an-error-creating-the-test-database-permission-deni) – Louis Mar 12 '19 at 14:41

3 Answers3

17

Django will use the same connection settings as in your settings.py for tests, but will use a different database (by default, test_mydb where your regular database is mydb).

You can change the django user permissions to create databases in the psql shell. See this related answer for more info.

=> ALTER USER myuser CREATEDB;

I don't know whether it's possible to restrict the permission so that the django user can only create the database test_mydb.

Community
  • 1
  • 1
Alasdair
  • 298,606
  • 55
  • 578
  • 516
4

As of 2020 a superior option to giving Django the ability to create databases per other answers is just to use the --keepdb flag when running your tests:

$ ./manage.py test --keepdb

This will keep the same db around between runs, which is much faster and doesn't require giving your django user CREATEDB permissions. More details here.

As @Alasdair previously noted, Django will use the same settings as your normal database by default but attempt to connect to or create a db at test_<your-database-name>. You can also customize the test DB's name with the TEST dict in your DATABASE settings.

Matt Sanders
  • 8,023
  • 3
  • 37
  • 49
0

For production, I faced error when executing the accepted answer solution. Something like this:

django.db.utils.OperationalError: cannot drop the currently open database

For me, according to this link, I need to do:

ALTER USER <your_user> CREATEDB;
CREATE DATABASE postgres;

It needs to be postgres for the test to work without raising django.db.utils.OperationalError