8

Not able to solve what is the error.

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

I keep on getting the Trace when i run any of the command below

  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.

Thanks in advance.

Pratibha
  • 1,730
  • 7
  • 27
  • 46
Arnold Laishram
  • 1,801
  • 2
  • 18
  • 25
  • Can you show your db settings? – Daniel Roseman Nov 16 '16 at 18:38
  • DATABASES = { '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 Nov 17 '16 at 04:39
  • This error seems also to occur when having long running tasks (4h) with DB operations at the beginning at and the end. Any idea how to overcome this? – Matthias Lohr Dec 19 '20 at 10:49

3 Answers3

1

This usually means that your PostgreSQL server is not running properly. You may want to restart it by

Linux

sudo /etc/init.d/postgresql restart

Windows

sc stop postgresql
sc start postgresql

Mac OS X

How to start PostgreSQL server on Mac OS X?

If restart fixes it, note that the root cause of the previous server failure still needs investigation.

azalea
  • 11,402
  • 3
  • 35
  • 46
0

I solved this problem uninstalling and installing postgresql again.

On Mac

Uninstall:

brew uninstall --force postgres

Install:

brew install postgres

PS: Change commands for Linux or Windows.

After, run makemigrations and migrate.

0

Happens when a process forks and connection established in parent process don't work in child processes.

I was using huggingface/tokenizers and BERT to get sentence embeddings and then inserting those into a Postgres database. The database connection was getting established first and then the tokenizer was forking which resulted in the same django.db.utils.OperationalError.

I solved it by disabling TOKENIZER_PARALLELISM. Though for your issue, upgrading to a higher version of Django might help as mentioned here.

Jishin K
  • 11
  • 1