AssertionError: 数据库连接未设置为 UTC

我已经使用相同的设置多次进行了服务器设置,但是这一次,我看到了错误消息。它甚至不允许迁移数据库。

System check identified no issues (0 silenced).
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/usr/lib/python3.9/threading.py", line 954, in _bootstrap_inner
self.run()
File "/usr/lib/python3.9/threading.py", line 892, in run
self._target(*self._args, **self._kwargs)
File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/utils/autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/core/management/commands/runserver.py", line 120, in inner_run
self.check_migrations()
File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/core/management/base.py", line 458, in check_migrations
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/db/migrations/executor.py", line 18, in __init__
self.loader = MigrationLoader(self.connection)
File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/db/migrations/loader.py", line 49, in __init__
self.build_graph()
File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/db/migrations/loader.py", line 212, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/db/migrations/recorder.py", line 77, in applied_migrations
return {(migration.app, migration.name): migration for migration in self.migration_qs}
File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/db/models/query.py", line 276, in __iter__
self._fetch_all()
File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/db/models/query.py", line 1261, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/db/models/query.py", line 57, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1170, in execute_sql
return list(result)
File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1569, in cursor_iter
for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):
File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1569, in <lambda>
for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):
File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/db/utils.py", line 97, in inner
return func(*args, **kwargs)
File "/home/datanal/datanal-samply/venv/lib/python3.9/site-packages/django/db/backends/postgresql/utils.py", line 6, in utc_tzinfo_factory
raise AssertionError("database connection isn't set to UTC")
AssertionError: database connection isn't set to UTC

这是我的设置,时区的 py。

LANGUAGE_CODE = 'en-us'


TIME_ZONE = 'UTC'


USE_I18N = True


USE_L10N = True


USE_TZ = True

操作系统: Ubuntu 21.04 Python 版本: 3.9.5 Django 版本: 3.0 PostgreSQL: 13.3

我也通过 另一个问题,但没有找到任何解决方案。有人能帮我完成这件事吗?我有多个服务器设置相同的代码没有改变任何东西和工作,但这次不是。

63922 次浏览

Psycopg2版本2.9的发布导致了这个错误,在 GitHub 中有解释:

Https://github.com/psycopg/psycopg2/issues/1293#issuecomment-862835147

Psycopg 2.9将传递给 tzinfo _ Factory 的值从 int 改为 timedelta。Django 2.2(可能更新,但我是在2.2上)有一个偏移量 = = 0和 timedelta (0)的检查!就会爆炸。

一种解决方案是将需求文件中的 psycopg2(如果使用独立包,则称为 psycopg2-binary)降级到2.9以下(例如 psycopg2>=2.8,<2.9)。

例如,你可以使用以下方法降级到 2.8.6:

pip install psycopg2==2.8.6

或者

pip install psycopg2-binary==2.8.6

如果你使用诗歌,你可以做 poetry add psycopg2@2.8.6来修复你的版本到 2.8.6

神经病2 释放历史

我通过升级姜戈而不是降级心理警察来解决这个问题。我不知道到底是哪个版本解决了这个问题,但是3.2确实解决了。

接受的答案现在已经过时了,如果您可以选择升级 Django,那么您应该决定不降级。

我也有同样的问题,我只是从 setings.py 文件中删除了这一行,就解决了这个问题

USE_TZ = True

这就是我所做的工作,让这一切在 Django 2.2.x(这是不兼容的 psycopg2>=2.9.0:

brew install libpq --build-from-source
brew install openssl
brew link openssl
export LDFLAGS="-L/opt/homebrew/opt/openssl@1.1/lib -L/opt/homebrew/opt/libpq/lib"
export CPPFLAGS="-I/opt/homebrew/opt/openssl@1.1/include -I/opt/homebrew/opt/libpq/include"
echo 'export PATH="/opt/homebrew/opt/openssl@3/bin:$PATH"' >> ~/.zshrc
brew install postgres
pip install psycopg2==2.8.6

我用的是 M1 Macbook 上的 BigSur。

确保您已经激活了您的虚拟环境,如果任何情况下您已经停用它(请确保您在虚拟环境中)

要激活您的虚拟环境,请使用以下命令:

对于 ubuntu 用户来说,使用旧版本的 django 安装 Psycopg2 = = 2.8.6,psycopg2-binary = = 2.8.6可能会产生某种错误。要解决这个问题,请尝试,

Sudo apt-get install gcc libpq-dev-y

Sudo apt-get install python3-dev python3-pip python3-venv python3-eler-y

管道3安装轮安装轮

这是一种解决方案,其他解决方案可以在下面的链接中找到。 Https://exerror.com/error-invalid-command-bdist_wheel/#:%7e:text=just%20use%20below%20command%20pip,command%20python%20setup.py%20bdist_wheel.&text=invalid%20command%20%27bdist_wheel%27-,to%20solve%20error%3a%20invalid%20command%20%27bdist_wheel%27%20error%20you%20just,command%20python%20setup.py%20bdist_wheel?