Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django / postgres setup for database creation, for running tests

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.
like image 707
blueFast Avatar asked Oct 20 '15 08:10

blueFast


1 Answers

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.

like image 107
Alasdair Avatar answered Oct 13 '22 10:10

Alasdair