Django creates a new test database every time you run the test cases, so I was wondering how I should setup the dev MySQL user so it can recreate the database without allowing this user to create or drop other databases.

Well, the answer was simple with MySQL since it doesn’t verify that the database exists before granting permissions on it. This resulted in the very convenient few statements below.

GRANT CREATE ON test_db_name.* TO dev_user@localhost;
GRANT ALL PRIVILEGES ON test_db_name.* TO dev_user@localhost;
FLUSH PRIVILEGES;

With these the dev_user can recreate this database as necessary. Have fun testing your applications 🙂

Back to blog...