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

python manage.py test appname

我得到这个错误:

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命令似乎可以工作。我的数据库设置在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.
    }
}

当前回答

超级用户帐户是保证测试顺利进行的最简单方法。因此,创建django用户su的一个更简单的方法是执行ALTER django WITH SUPERUSER。

更多信息https://www.postgresql.org/docs/current/sql-alteruser.html

其他回答

对于Postgres,用户必须具有createdb权限。

ALTER ROLE miriam CREATEDB;

请参阅此文档:https://docs.djangoproject.com/en/2.0/topics/testing/overview/#the-test-database

也许您将测试设置为暂停模式或作为后台工作。尝试使用bash shell中的fg命令。

超级用户帐户是保证测试顺利进行的最简单方法。因此,创建django用户su的一个更简单的方法是执行ALTER django WITH SUPERUSER。

更多信息https://www.postgresql.org/docs/current/sql-alteruser.html

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

1.Open mysite mysite / settings.py

您的数据库设置应该有一个额外的“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.使用mysql命令提示符或mysql工作台输入下面的命令,将所有权限赋予settings.py中指定的用户

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

现在你可以运行下面的命令:

python manage.py test polls

您也可以手动创建一个测试数据库,但将test键放在settings.py中

然后用值'default'填充传递MIRROR键,这样你就可以测试默认db的精确副本,或者任何你喜欢的db。

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'finance',
        'USER': 'django',
        'TEST': {
             'MIRROR': 'default',
        },
        'PASSWORD': 'mydb123',
        'HOST': '127.0.0.1',
        'PORT': '',
    }
}

你也可以查看django postgres的高级文档