Error: psycopg2.OperationalError: SSL connection has been closed unexpectedly

I’m running a baserow instance using the AIO docker image deployed in a digital ocean droplet. The instance has been fine for many weeks but suddenly I’ve been getting errors.

I just hit this API endpoint on my instance: /api/database/fields/table/4059/to get some columns but failed. Its not limited to that API call, also happens when updating rows via API. It also doesn’t just happen on just 1 table or database, happens to many but not always. I don’t know what’s causing it

I have baserow v1.23 and earlier today, I upgraded to 1.29 to see if it fixes the issue - it didn’t. When I upgraded to v1.29 I put MIGRATE_ON_STARTUP=true and BASEROW_TRIGGER_SYNC_TEMPLATES_AFTER_MIGRATION=false – I had both as false because I’ve been testing a few configurations when I started self-hosting.

I’m not too familiar with python so I have no Idea why this happens.

Database is in Supabase
Digital Ocean storage/s3
self-hosted in a Droplet with docker using the all-in-one image

ERROR 2024-11-15 15:09:30,908 django.request.log_response:241- Internal Server Error: /api/database/fields/table/4059/
 [BACKEND][2024-11-15 15:09:50] Traceback (most recent call last):
 [BACKEND][2024-11-15 15:09:50]   File "/baserow/venv/lib/python3.11/site-packages/django/db/backends/utils.py", line 89, in _execute
 [BACKEND][2024-11-15 15:09:50]     return self.cursor.execute(sql, params)
 [BACKEND][2024-11-15 15:09:50]            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 [BACKEND][2024-11-15 15:09:50] psycopg2.OperationalError: SSL connection has been closed unexpectedly

full log:

[BACKEND][2024-11-15 15:09:50] ERROR 2024-11-15 15:09:30,908 django.request.log_response:241- Internal Server Error: /api/database/fields/table/4059/
 [BACKEND][2024-11-15 15:09:50] Traceback (most recent call last):
 [BACKEND][2024-11-15 15:09:50]   File "/baserow/venv/lib/python3.11/site-packages/django/db/backends/utils.py", line 89, in _execute
 [BACKEND][2024-11-15 15:09:50]     return self.cursor.execute(sql, params)
 [BACKEND][2024-11-15 15:09:50]            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 [BACKEND][2024-11-15 15:09:50] psycopg2.OperationalError: SSL connection has been closed unexpectedly
 [BACKEND][2024-11-15 15:09:50]
 [BACKEND][2024-11-15 15:09:50]
 [BACKEND][2024-11-15 15:09:50] The above exception was the direct cause of the following exception:
 [BACKEND][2024-11-15 15:09:50]
 [BACKEND][2024-11-15 15:09:50] Traceback (most recent call last):
 [BACKEND][2024-11-15 15:09:50]   File "/baserow/venv/lib/python3.11/site-packages/asgiref/sync.py", line 486, in thread_handler
 [BACKEND][2024-11-15 15:09:50]     raise exc_info[1]
 [BACKEND][2024-11-15 15:09:50]   File "/baserow/venv/lib/python3.11/site-packages/django/core/handlers/exception.py", line 43, in inner
 [BACKEND][2024-11-15 15:09:50]     response = await get_response(request)
 [BACKEND][2024-11-15 15:09:50]                ^^^^^^^^^^^^^^^^^^^^^^^^^^^
 [BACKEND][2024-11-15 15:09:50]   File "/baserow/venv/lib/python3.11/site-packages/asgiref/sync.py", line 486, in thread_handler
 [BACKEND][2024-11-15 15:09:50]     raise exc_info[1]
 [BACKEND][2024-11-15 15:09:50]   File "/baserow/venv/lib/python3.11/site-packages/django/core/handlers/base.py", line 253, in _get_response_async
 [BACKEND][2024-11-15 15:09:50]     response = await wrapped_callback(
 [BACKEND][2024-11-15 15:09:50]                ^^^^^^^^^^^^^^^^^^^^^^^
 [BACKEND][2024-11-15 15:09:50]   File "/baserow/venv/lib/python3.11/site-packages/asgiref/sync.py", line 448, in __call__
 [BACKEND][2024-11-15 15:09:50]     ret = await asyncio.wait_for(future, timeout=None)
 [BACKEND][2024-11-15 15:09:50]           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 [BACKEND][2024-11-15 15:09:50]   File "/usr/lib/python3.11/asyncio/tasks.py", line 442, in wait_for
 [BACKEND][2024-11-15 15:09:50]     return await fut
 [BACKEND][2024-11-15 15:09:50]            ^^^^^^^^^
 [BACKEND][2024-11-15 15:09:50]   File "/baserow/venv/lib/python3.11/site-packages/asgiref/current_thread_executor.py", line 22, in run
 [BACKEND][2024-11-15 15:09:50]     result = self.fn(*self.args, **self.kwargs)
 [BACKEND][2024-11-15 15:09:50]              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 [BACKEND][2024-11-15 15:09:50]   File "/baserow/venv/lib/python3.11/site-packages/asgiref/sync.py", line 490, in thread_handler
 [BACKEND][2024-11-15 15:09:50]     return func(*args, **kwargs)
 [BACKEND][2024-11-15 15:09:50]            ^^^^^^^^^^^^^^^^^^^^^
 [BACKEND][2024-11-15 15:09:50]   File "/baserow/venv/lib/python3.11/site-packages/django/views/decorators/csrf.py", line 55, in wrapped_view
 [BACKEND][2024-11-15 15:09:50]     return view_func(*args, **kwargs)
 [BACKEND][2024-11-15 15:09:50]            ^^^^^^^^^^^^^^^^^^^^^^^^^^
 [BACKEND][2024-11-15 15:09:50]   File "/baserow/venv/lib/python3.11/site-packages/django/views/generic/base.py", line 103, in view
 [BACKEND][2024-11-15 15:09:50]     return self.dispatch(request, *args, **kwargs)
 [BACKEND][2024-11-15 15:09:50]            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 [BACKEND][2024-11-15 15:09:50]   File "/baserow/venv/lib/python3.11/site-packages/rest_framework/views.py", line 509, in dispatch
 [BACKEND][2024-11-15 15:09:50]     response = self.handle_exception(exc)
 [BACKEND][2024-11-15 15:09:50]                ^^^^^^^^^^^^^^^^^^^^^^^^^^
 [BACKEND][2024-11-15 15:09:50]   File "/baserow/venv/lib/python3.11/site-packages/rest_framework/views.py", line 469, in handle_exception
 [BACKEND][2024-11-15 15:09:50]     self.raise_uncaught_exception(exc)
 [BACKEND][2024-11-15 15:09:50]   File "/baserow/venv/lib/python3.11/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
 [BACKEND][2024-11-15 15:09:50]     raise exc
 [BACKEND][2024-11-15 15:09:50]   File "/baserow/venv/lib/python3.11/site-packages/rest_framework/views.py", line 497, in dispatch
 [BACKEND][2024-11-15 15:09:50]     self.initial(request, *args, **kwargs)
 [BACKEND][2024-11-15 15:09:50]   File "/baserow/venv/lib/python3.11/site-packages/rest_framework/views.py", line 414, in initial
 [BACKEND][2024-11-15 15:09:50]     self.perform_authentication(request)
 [BACKEND][2024-11-15 15:09:50]   File "/baserow/venv/lib/python3.11/site-packages/rest_framework/views.py", line 324, in perform_authentication
 [BACKEND][2024-11-15 15:09:50]     request.user
 [BACKEND][2024-11-15 15:09:50]   File "/baserow/venv/lib/python3.11/site-packages/rest_framework/request.py", line 227, in user
 [BACKEND][2024-11-15 15:09:50]     self._authenticate()
 [BACKEND][2024-11-15 15:09:50]   File "/baserow/venv/lib/python3.11/site-packages/rest_framework/request.py", line 380, in _authenticate
 [BACKEND][2024-11-15 15:09:50]     user_auth_tuple = authenticator.authenticate(self)
 [BACKEND][2024-11-15 15:09:50]                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 [BACKEND][2024-11-15 15:09:50]   File "/baserow/backend/src/baserow/contrib/database/api/tokens/authentications.py", line 40, in authenticate
 [BACKEND][2024-11-15 15:09:50]     token = handler.get_by_key(decoded_key)
 [BACKEND][2024-11-15 15:09:50]             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 [BACKEND][2024-11-15 15:09:50]   File "/baserow/backend/src/baserow/contrib/database/tokens/handler.py", line 45, in get_by_key
 [BACKEND][2024-11-15 15:09:50]     token = Token.objects.select_related("workspace").get(key=key)
 [BACKEND][2024-11-15 15:09:50]             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 [BACKEND][2024-11-15 15:09:50]   File "/baserow/venv/lib/python3.11/site-packages/django/db/models/query.py", line 646, in get
 [BACKEND][2024-11-15 15:09:50]     num = len(clone)
 [BACKEND][2024-11-15 15:09:50]           ^^^^^^^^^^
 [BACKEND][2024-11-15 15:09:50]   File "/baserow/venv/lib/python3.11/site-packages/django/db/models/query.py", line 376, in __len__
 [BACKEND][2024-11-15 15:09:50]     self._fetch_all()
 [BACKEND][2024-11-15 15:09:50]   File "/baserow/venv/lib/python3.11/site-packages/django/db/models/query.py", line 1867, in _fetch_all
 [BACKEND][2024-11-15 15:09:50]     self._result_cache = list(self._iterable_class(self))
 [BACKEND][2024-11-15 15:09:50]                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 [BACKEND][2024-11-15 15:09:50]   File "/baserow/venv/lib/python3.11/site-packages/django/db/models/query.py", line 87, in __iter__
 [BACKEND][2024-11-15 15:09:50]     results = compiler.execute_sql(
 [BACKEND][2024-11-15 15:09:50]               ^^^^^^^^^^^^^^^^^^^^^
 [BACKEND][2024-11-15 15:09:50]   File "/baserow/venv/lib/python3.11/site-packages/django/db/models/sql/compiler.py", line 1398, in execute_sql
 [BACKEND][2024-11-15 15:09:50]     cursor.execute(sql, params)
 [BACKEND][2024-11-15 15:09:50]   File "/baserow/venv/lib/python3.11/site-packages/django/db/backends/utils.py", line 67, in execute
 [BACKEND][2024-11-15 15:09:50]     return self._execute_with_wrappers(
 [BACKEND][2024-11-15 15:09:50]            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 [BACKEND][2024-11-15 15:09:50]   File "/baserow/venv/lib/python3.11/site-packages/django/db/backends/utils.py", line 80, in _execute_with_wrappers
 [BACKEND][2024-11-15 15:09:50]     return executor(sql, params, many, context)
 [BACKEND][2024-11-15 15:09:50]            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 [BACKEND][2024-11-15 15:09:50]   File "/baserow/venv/lib/python3.11/site-packages/django/db/backends/utils.py", line 84, in _execute
 [BACKEND][2024-11-15 15:09:50]     with self.db.wrap_database_errors:
 [BACKEND][2024-11-15 15:09:50]   File "/baserow/venv/lib/python3.11/site-packages/django/db/utils.py", line 91, in __exit__
 [BACKEND][2024-11-15 15:09:50]     raise dj_exc_value.with_traceback(traceback) from exc_value
 [BACKEND][2024-11-15 15:09:50]   File "/baserow/venv/lib/python3.11/site-packages/django/db/backends/utils.py", line 89, in _execute
 [BACKEND][2024-11-15 15:09:50]     return self.cursor.execute(sql, params)
 [BACKEND][2024-11-15 15:09:50]            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 [BACKEND][2024-11-15 15:09:50] django.db.utils.OperationalError: SSL connection has been closed unexpectedly

Not only do I get the above errors for hitting the backend API, I also get these from the EXPORT_WORKER – SAME ERROR: SSL connection has been closed unexpectedly
but I suspect it has nothing to do with SSL. Might it have something to do with the database instead?

 [EXPORT_WORKER][2024-11-15 15:33:20] [2024-11-15 15:33:00,007: INFO/MainProcess] Task baserow.core.notifications.tasks.singleton_send_instant_notifications_summary_by_email[601cd831-0d9f-4849-87e0-2193b59bccb8] received
 [EXPORT_WORKER][2024-11-15 15:33:20] [2024-11-15 15:33:20,051: WARNING/ForkPoolWorker-1] --- Logging error ---
 [EXPORT_WORKER][2024-11-15 15:33:20] [2024-11-15 15:33:20,052: WARNING/ForkPoolWorker-1] Traceback (most recent call last):
 [EXPORT_WORKER][2024-11-15 15:33:20] [2024-11-15 15:33:20,052: WARNING/ForkPoolWorker-1]   File "/baserow/venv/lib/python3.11/site-packages/django/db/backends/utils.py", line 89, in _execute
 [EXPORT_WORKER][2024-11-15 15:33:20]     return self.cursor.execute(sql, params)
 [EXPORT_WORKER][2024-11-15 15:33:20]            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 [EXPORT_WORKER][2024-11-15 15:33:20] [2024-11-15 15:33:20,052: WARNING/ForkPoolWorker-1] psycopg2.OperationalError: SSL connection has been closed unexpectedly

Hi @xyxy,

what was and is now your PostgreSQL version? Do you by any chance need to deal with upgrading your database PostgreSQL upgrade process?

Can you connect to your database outside of Baserow? Are you sure that you are using the same host and credentials?

Happens to me too, on trying to create a million-row table with “Sync PostgreSQL table”. Goes up till 16% then I see this below in the log. PostgresSQL 17 and freshly installed self-hosted Baserow.

[EXPORT_WORKER][2024-12-11 14:48:21] [2024-12-11 14:48:21,905: ERROR/ForkPoolWorker-1] Task baserow.core.jobs.tasks.run_async_job[62b553af-8f06-4792-9d18-1c82a7bf5495] raised unexpected: OperationalError(‘SSL connection has been closed unexpectedly\n’)