【发布时间】:2022-01-16 21:16:02
【问题描述】:
我正在尝试为我的 Django 项目运行测试。我前段时间写了这个项目,当时我有不同的设置并且测试通过了。现在我更改了设置并使用 Heroku Postgres 数据库将其部署在 Heroku 上。一切正常,除了我无法运行测试。我尝试了许多不同的设置,但没有任何效果。大多数时候我收到此错误:创建数据库的权限被拒绝
我的最后一个设置是按照这个article on medium 的指示进行的 基本上我已经添加了第二个 Heroku Postgres 数据库,添加如下设置(但使用我的 heroku 数据库的有效变量):
if 'test' in sys.argv:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'd7osdssadag0ugv5',
'USER': 'lhwwasadqlgjra',
'PASSWORD': '1524f48a2ce41177c4ssdadasd3a11680b735302d14979d312ff36',
'HOST': 'ec2-54-75-2326-118.eu-west-1.compute.amazonaws.com',
'PORT': 5432,
'TEST': {
'NAME': 'd7osdssadag0ugv5',
}
}
}
else:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'd7hasadas9hqts5',
'USER': 'nybkduadsdgqot',
'PASSWORD': 'bb535b9cdsfsdfdsfdsfac54851f267444dd8cc230b2a786ab9f446',
'HOST': 'ec2-54-247-132-38.eu-west-1.compute.amazonaws.com',
'PORT': 5432,
'TEST': {
'NAME': 'd7hasadas9hqts5',
}
}
}
然后在我的 venv 中运行 python manage.py test --keepdb。然后我得到一个错误:
RuntimeWarning: Normally Django will use a connection to the 'postgres' database to avoid running initialization queries against the production database when it's not needed (for example, when running tests). Django was unable to create a connection to the 'postgres' database and will use the first PostgreSQL database instead.
RuntimeWarning
Got an error creating the test database: permission denied to create database
我也尝试了this article中的建议
您有什么想法可以解决这个错误吗?我不太了解姜戈。我经常玩它。
我正在使用:Python 3.6.9、Django 3.0.3、Heroku Postgresql 爱好开发
编辑:
我不确定这是否是我的设置数据库的问题。
现在,当我注释掉所有关于 DATABASES 的设置并运行 python manage.py runserver 时,我的开发服务器正常启动,并且我可以访问我之前设置的数据库(即使在重新启动计算机之后)。这看起来像实际设置没有效果(??)有什么想法吗?
Django version 3.0.3, using settings 'forumproject.settings'
Starting development server at http://127.0.0.1:8000/
【问题讨论】:
标签: django database postgresql testing heroku