django test app error - Got an error creating the test database: permission denied to create database

后端 未结 9 934
甜味超标
甜味超标 2020-12-22 16:53

When I try to test any app with command (I noticed it when I tried to deploy myproject using fabric, which uses this command):

python manage.py test appname
         


        
9条回答
  •  春和景丽
    2020-12-22 17:29

    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.

提交回复
热议问题