当我尝试使用命令测试任何应用程序时(我在尝试使用使用此命令的结构部署我的项目时注意到了这一点):
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 运行测试套件时,它会创建一个新数据库,在您的情况下为 test_finance
.用户名 django
的 postgres 用户没有创建数据库的权限,因此出现错误消息。
当您运行 migrate
时或 syncdb
, Django 不会尝试创建 finance
数据库,所以你不会得到任何错误。
您可以通过在 postgres shell 中以 super 用户身份运行以下命令来为 django 用户添加 createdb 权限(提示 this stack overflow answer)。
=> ALTER USER django CREATEDB;
注意: ALTER USER <username> CREATEDB;
中使用的用户名命令需要匹配 Django 设置文件中的数据库用户。在这种情况下,原始发布者的用户为 django
上面的答案。
关于python - django 测试应用程序错误 - 创建测试数据库时出错 : permission denied to create database,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14186055/