[PYTHON] OperationalError: (psycopg2.OperationalError) gave an error

Introduction

When I was running the flask app with psycopg2, I got the following error.

OperationalError: (psycopg2.OperationalError) server closed the connection unexpectedly
    This probably means the server terminated abnormally
    before or while processing the request.

Solution

from flask_sqlalchemy import SQLAlchemy as _BaseSQLAlchemy

class SQLAlchemy(_BaseSQLAlchemy):
    def apply_pool_defaults(self, app, options):
        super(SQLAlchemy, self).apply_pool_defaults(self, app, options)
        options["pool_pre_ping"] = True

db = SQLAlchemy(app)

It was solved by putting the above program. The reference URL below also lists other solutions, so if you are good at English, please take a look.

Reference URL

https://stackoverflow.com/questions/55457069/how-to-fix-operationalerror-psycopg2-operationalerror-server-closed-the-conn

https://github.com/pallets/flask-sqlalchemy/issues/589#issuecomment-361075700

Recommended Posts

OperationalError: (psycopg2.OperationalError) gave an error
nltk.MaxentClassifier.train () throws an error.
An error occurred in scikit-learn imputer