Django.db.utils.OperationalError: 服务器意外关闭了连接。

8

无法解决错误是什么。

django.db.utils.OperationalError: server closed the connection unexpectedly
    This probably means the server terminated abnormally
    before or while processing the request.

无论我运行以下任何命令,都会收到Trace的提示:

  1. python manage.py makemigrations
  2. python manage.py runserver
Unhandled exception in thread started by <function wrapper at     0x0000000003DAC4A8>
Traceback (most recent call last): File "C:\Python27\lib\site-packages\django\utils\autoreload.py", line 226, in wrapper fn(*args, **kwargs)
    File "C:\Python27\lib\site packages\django\core\management\commands\runserver.py", line 124, in inner_run
    self.check_migrations()
  File "C:\Python27\lib\site-packages\django\core\management\base.py", line 437, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
    File "C:\Python27\lib\site-packages\django\db\migrations\executor.py", line 20, in __init__
    self.loader = MigrationLoader(self.connection)
    File "C:\Python27\lib\site-packages\django\db\migrations\loader.py", line 52, in __init__
    self.build_graph()
    File "C:\Python27\lib\site-packages\django\db\migrations\loader.py", line 203, in build_graph
    self.applied_migrations = recorder.applied_migrations()
    File "C:\Python27\lib\site-packages\django\db\migrations\recorder.py", line 65, in applied_migrations
    self.ensure_schema()
    File "C:\Python27\lib\site-packages\django\db\migrations\recorder.py", line 52, in ensure_schema
    if self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()):
    File "C:\Python27\lib\site-packages\django\db\backends\base\base.py", line 231, in cursor
    cursor = self.make_debug_cursor(self._cursor())
    File "C:\Python27\lib\site-packages\django\db\backends\base\base.py", line 204, in _cursor
    self.ensure_connection()
    File "C:\Python27\lib\site-packages\django\db\backends\base\base.py", line 199, in ensure_connection
    self.connect()
    File "C:\Python27\lib\site-packages\django\db\utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
    File "C:\Python27\lib\site-packages\django\db\backends\base\base.py", line 199, in ensure_connection
    self.connect()
    File "C:\Python27\lib\site-packages\django\db\backends\base\base.py", line 171, in connect
    self.connection = self.get_new_connection(conn_params)
    File "C:\Python27\lib\site-packages\django\db\backends\postgresql\base.py", line 176, in get_new_connection
    connection = Database.connect(**conn_params)
    File "C:\Python27\lib\site-packages\psycopg2\__init__.py", line 164, in connect
    conn = _connect(dsn, connection_factory=connection_factory, async=async)
django.db.utils.OperationalError: server closed the connection unexpectedly
        This probably means the server terminated abnormally
        before or while processing the request.

提前感谢您。

你能展示一下你的数据库设置吗? - Daniel Roseman
数据库 = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'dab_name', 'USER': 'my_user_name', 'PASSWORD': 'my_password', 'HOST': 'ec2-54-225-242.compute-1.amazonaws.com', 'PORT': '5432', } } - Arnold Laishram
这个错误似乎也会在长时间运行的任务(4小时)在开始和结束时进行数据库操作时发生。有什么办法可以克服这个问题吗? - Matthias Lohr
3个回答

1
这通常意味着您的PostgreSQL服务器未正常运行。您可能需要通过以下方式重新启动它:

Linux

sudo /etc/init.d/postgresql restart

Windows

sc stop postgresql
sc start postgresql

Mac OS X

如何在Mac OS X上启动PostgreSQL服务器?

如果重启可以解决问题,请注意之前服务器故障的根本原因仍需调查。


0

当一个进程分叉并且在父进程中建立的连接在子进程中不起作用时会发生这种情况。

我使用了 huggingface/tokenizersBERT 来获取句子嵌入,然后将它们插入到 Postgres 数据库中。数据库连接首先被建立,然后是分词器分叉,导致出现相同的 django.db.utils.OperationalError

我通过 禁用 TOKENIZER_PARALLELISM 来解决了这个问题。虽然对于你的问题,升级到更高版本的 Django 可能会有所帮助,如 此处 所述。


0

我通过卸载并重新安装PostgreSQL来解决这个问题。

在Mac上

卸载:

brew uninstall --force postgres

安装:

brew install postgres

PS:更改Linux或Windows命令。

然后,运行makemigrations和migrate。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接