Cannot connect to database

Hello, I keep getting errors while connecting to my database (I’m using psycopg in Python) such as:

consuming input failed: terminating connection due to administrator command
SSL connection has been closed unexpectedly

I have implemented a retry strategy to make sure enough time is left for the database to startup, but even when it is not idle I get those seemingly random errors.

Can you help me understanding what exactly is happening? Thank you!

Hi!

We shut down databases if there are no queries for the last 5 minutes and such error message may appear if we shut it down. However, open connection should prevent this from happening, so that needs some investigation. Feel free to drop an email to support@ with the project name and we’ll have a look.

Hi,

I’m receiving similar errors that I suspect is caused by the same issue and hope that you’ve found a solution. My case is as follows:

  • Back end Python code running in an AWS lambda connects to Neon (using sqlalchemy) and runs it’s queries (successfully)
  • 60s later (our Neon compute shut down time) Neon compute goes to idle
  • If another request is made to our back-end after the Neon compute time out but while the lambdas are still warm (5-15 minutes) then we receive the following error
psycopg2.OperationalError: SSL connection has been closed unexpectedly

Is there a way we can keep the connection alive while the lambda remains warm without increasing the Neon compute timeout duration or a way to make the lambda reopen the connection if it’s been closed?

The solution in my case was to replace

engine = create_engine(SQLALCHEMY_DATABASE_URL)

with

engine = create_engine(SQLALCHEMY_DATABASE_URL, pool_pre_ping=True)

as described here: Connection Pooling — SQLAlchemy 1.3 Documentation

2 Likes