django测试应用程序错误——创建测试数据库时出错:拒绝创建数据库。

时间:2022-05-12 22:48:26

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

当我尝试用命令来测试任何应用程序时(当我尝试使用fabric来部署myproject时,我注意到它使用了这个命令):

python manage.py test appname

I get this error:

我得到这个错误:

Creating test database for alias 'default'...
Got an error creating the test database: permission denied to create database

Type 'yes' if you would like to try deleting the test database 'test_finance', or 'no' to cancel

syncdb command seems to work. My database settings in settings.py:

syncdb命令似乎起作用了。我的数据库设置在settings.py:

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.
    }
}

4 个解决方案

#1


241  

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.

当Django运行测试套件时,它会创建一个新的数据库,在您的案例test_finance中。使用用户名django的postgres用户没有创建数据库的权限,因此出现了错误消息。

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

当您运行迁移或syncdb时,Django不会尝试创建财务数据库,因此不会出现任何错误。

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

您可以通过在postexit shell中运行以下命令,将createdb权限添加到django用户,作为一个超级用户(此堆栈溢出答案的hat提示)。

=> 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.

注意:用户名中使用的用户名 <用户名> CREATEDB;命令需要在Django设置文件中与数据库用户匹配。在这种情况下,最初的海报,让用户被django上面的答案。

#2


7  

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:

我找到了解决你问题的有趣方法。实际上,对于MySQL,您可以为非现有数据库授予特权。因此,您可以在设置中为测试数据库添加名称“test_finance”:

    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 shell作为根用户:

mysql -u root -p

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

现在将所有权限授予MySQL中这个非现有数据库:

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

Now Django will start tests without any problems.

现在Django将开始测试,没有任何问题。

#3


1  

If database is mysql then these two changes will get the things done.

如果数据库是mysql,那么这两个更改将完成任务。

1.Open mysite/mysite/settings.py

1。打开mysite / mysite / settings.py

Your database settings should have an additional TEST block as shown with projectname_test.

您的数据库设置应该有一个额外的测试块,如projectname_test所示。

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'myproject',
        'USER': 'chandan',
        'PASSWORD': 'root',
        'HOST': 'localhost',
        'PORT': '3306',
        'TEST': {
            'NAME': 'myproject_test',
        },
    }
}

2.Type the below command using mysql command prompt or mysql workbench to give all privilages to the user specified in settings.py

2。使用mysql命令提示符或mysql工作台键入下面的命令,以便将所有的privilage交给settings.py中指定的用户。

GRANT ALL PRIVILEGES ON myproject_test.* TO 'chandan'@'localhost';

Now you can run python manage.py test polls.

现在可以运行python管理了。py测试民意调查。

#4


0  

In the case of Postgres, the user must have createdb permission.

对于Postgres,用户必须获得createdb权限。

ALTER ROLE miriam CREATEDB;

See this documentation: https://docs.djangoproject.com/en/2.0/topics/testing/overview/#the-test-database

看到这个文档:https://docs.djangoproject.com/en/2.0/topics/testing/overview/测试数据库

#1


241  

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.

当Django运行测试套件时,它会创建一个新的数据库,在您的案例test_finance中。使用用户名django的postgres用户没有创建数据库的权限,因此出现了错误消息。

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

当您运行迁移或syncdb时,Django不会尝试创建财务数据库,因此不会出现任何错误。

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

您可以通过在postexit shell中运行以下命令,将createdb权限添加到django用户,作为一个超级用户(此堆栈溢出答案的hat提示)。

=> 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.

注意:用户名中使用的用户名 <用户名> CREATEDB;命令需要在Django设置文件中与数据库用户匹配。在这种情况下,最初的海报,让用户被django上面的答案。

#2


7  

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:

我找到了解决你问题的有趣方法。实际上,对于MySQL,您可以为非现有数据库授予特权。因此,您可以在设置中为测试数据库添加名称“test_finance”:

    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 shell作为根用户:

mysql -u root -p

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

现在将所有权限授予MySQL中这个非现有数据库:

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

Now Django will start tests without any problems.

现在Django将开始测试,没有任何问题。

#3


1  

If database is mysql then these two changes will get the things done.

如果数据库是mysql,那么这两个更改将完成任务。

1.Open mysite/mysite/settings.py

1。打开mysite / mysite / settings.py

Your database settings should have an additional TEST block as shown with projectname_test.

您的数据库设置应该有一个额外的测试块,如projectname_test所示。

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'myproject',
        'USER': 'chandan',
        'PASSWORD': 'root',
        'HOST': 'localhost',
        'PORT': '3306',
        'TEST': {
            'NAME': 'myproject_test',
        },
    }
}

2.Type the below command using mysql command prompt or mysql workbench to give all privilages to the user specified in settings.py

2。使用mysql命令提示符或mysql工作台键入下面的命令,以便将所有的privilage交给settings.py中指定的用户。

GRANT ALL PRIVILEGES ON myproject_test.* TO 'chandan'@'localhost';

Now you can run python manage.py test polls.

现在可以运行python管理了。py测试民意调查。

#4


0  

In the case of Postgres, the user must have createdb permission.

对于Postgres,用户必须获得createdb权限。

ALTER ROLE miriam CREATEDB;

See this documentation: https://docs.djangoproject.com/en/2.0/topics/testing/overview/#the-test-database

看到这个文档:https://docs.djangoproject.com/en/2.0/topics/testing/overview/测试数据库