close

[Solved] django test app error – Got an error creating the test database: permission denied to create database

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error django test app error – Got an error creating the test database: permission denied to create database in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How django test app error – Got an error creating the test database: permission denied to create database Error Occurs?

Today I get the following error django test app error – Got an error creating the test database: permission denied to create database in python.

How To Solve django test app error – Got an error creating the test database: permission denied to create database Error ?

  1. How To Solve django test app error – Got an error creating the test database: permission denied to create database Error ?

    To Solve django test app error – Got an error creating the test database: permission denied to create database Error When you run migrate or syncdb, Django does not try to create the finance database, so you don't get any errors.

  2. django test app error – Got an error creating the test database: permission denied to create database

    To Solve django test app error – Got an error creating the test database: permission denied to create database Error When you run migrate or syncdb, Django does not try to create the finance database, so you don't get any errors.

Solution 1

When Django runs the test suite, it creates a new database, in your case test_finance. The postgres user with username django does not have permission to create a database, hence the error message.

When you run migrate or syncdb, Django does not try to create the finance database, so you don’t get any errors.

You can add the createdb permission to the django user by running the following command in the postgres shell as a superuser (hat tip to this stack overflow answer).

=> ALTER USER django CREATEDB;

Note: The username used in the ALTER USER <username> CREATEDB; command needs to match the database user in your Django settings files. In this case, the original poster, had the user as django the above answer.

Solution 2

I have found interesting solution to your problem.
In fact for MySQL you can grant privileges for non-existing database.
So you can add name ‘test_finance’ for your test database in your settings:

    DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'finance',                      # Or path to database file if using sqlite3.
        'USER': 'django',                      # Not used with sqlite3.
        'PASSWORD': 'mydb123',                  # Not used with sqlite3.
        'HOST': '127.0.0.1',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
        'TEST': {
            'NAME': 'test_finance',
        },
    }
}

start MySQL shell as the root user:

mysql -u root -p

and now grant all privileges to this non-existing database in MySQL:

GRANT ALL PRIVILEGES ON test_finance.* TO 'django'@'localhost';

Now Django will start tests without any problems.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read