django 测试应用程序错误 - 创建测试数据库时出错:创建数据库的权限被拒绝

2024-01-12

当我尝试使用命令测试任何应用程序时(当我尝试使用 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 运行测试套件时,它会根据您的情况创建一个新数据库test_finance。具有用户名的 postgres 用户django没有创建数据库的权限,因此出现错误消息。

当你跑步时migrate or syncdb, Django 不会尝试创建finance数据库,这样就不会出现任何错误。

您可以通过在 postgres shell 中以超级用户身份运行以下命令来向 django 用户添加 createdb 权限(提示这个堆栈溢出答案 https://stackoverflow.com/questions/8988495/postgres-user-create-database/8989722#8989722).

=> ALTER USER django CREATEDB;

Note:中使用的用户名ALTER USER <username> CREATEDB;命令需要与 Django 设置文件中的数据库用户匹配。在这种情况下,原始海报将用户设置为django上述答案。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

django 测试应用程序错误 - 创建测试数据库时出错:创建数据库的权限被拒绝 的相关文章

随机推荐