python - django 测试应用程序错误 - 创建测试数据库时出错 : permission

当我尝试使用命令测试任何应用程序时(我在尝试使用使用此命令的结构部署我的项目时注意到了这一点):

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/

相关文章:

linux - 如何在单个命令中创建目录并授予权限

python - 检查字符串是否可以在 Python 中转换为 float

linux - 发送电子邮件的 Shell 脚本

linux - 我的环境变量是什么?

linux - 对于高级用户来说,有哪些优秀的 Linux/Unix 书籍?

python - 为什么 print 在 lambda 中不起作用?

python - 如何在 Python 中获取 PATH 环境变量分隔符?

python - 为什么 Python 3.x 的 super() 有魔力?

python - 如何让 Pylint 识别 NumPy 成员?

python - Queue.Queue 与 collections.deque