当我尝试使用命令测试任何应用程序时(当我尝试使用使用此命令的结构部署 myproject 时,我注意到了这一点):
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.
}
}
原文由 Andrius 发布,翻译遵循 CC BY-SA 4.0 许可协议
当 Django 运行测试套件时,它会创建一个新数据库,在您的情况下是
test_finance
。用户名为django
的 postgres 用户无权创建数据库,因此出现错误消息。当您运行
migrate
或syncdb
时,Django 不会尝试创建finance
数据库,因此您不会收到任何错误。您可以通过在 postgres shell 中以超级用户身份运行以下命令来向 django 用户添加 createdb 权限( 对此堆栈溢出答案 的提示)。
注意:
ALTER USER <username> CREATEDB;
命令中使用的用户名需要与 Django 设置文件中的数据库用户匹配。在这种情况下,原始发布者的用户为django
上述答案。