当我try 使用命令测试任何应用程序时(我在try 使用fabric部署myproject时注意到了这一点,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命令似乎奏效了.我的数据库设置在"设置"中.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用户没有创建数据库的权限,因此会显示错误消息.

运行migratesyncdb时,Django不会try 创建finance数据库,因此不会出现任何错误.

通过以超级用户身份在postgres shell中运行以下命令(hat tip to this stack overflow answer),可以向django用户添加createdb权限.

=> ALTER USER django CREATEDB;

Note: ALTER USER <username> CREATEDB;命令中使用的用户名需要与Django设置文件中的数据库用户匹配.在本例中,原始海报让用户看到了上述答案.

Python相关问答推荐

将DF中的名称与另一DF拆分并匹配并返回匹配的公司

如何在Windows上用Python提取名称中带有逗号的文件?

Vectorize多个头寸的止盈/止盈回溯测试pythonpandas

从numpy数组和参数创建收件箱

在Wayland上使用setCellWidget时,try 编辑QTable Widget中的单元格时,PyQt 6崩溃

如何将Docker内部运行的mariadb与主机上Docker外部运行的Python脚本连接起来

Python,Fitting into a System of Equations

如何使用表达式将字符串解压缩到Polars DataFrame中的多个列中?

Streamlit应用程序中的Plotly条形图中未正确显示Y轴刻度

从一个系列创建一个Dataframe,特别是如何重命名其中的列(例如:使用NAs/NaN)

根据列值添加时区

手动设置seborn/matplotlib散点图连续变量图例中显示的值

从旋转的DF查询非NaN值

Python pint将1/华氏度转换为1/摄氏度°°

如何从比较函数生成ngroup?

如何在PYTHON中向单元测试S Side_Effect发送额外参数?

在不中断格式的情况下在文件的特定部分插入XML标签

如何通过函数的强式路径动态导入函数?

正则表达式反向查找

如何将ManyToManyfield用于Self类