Django / postgres setup for database creation, for running tests

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.


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.