Cannot create a database with SqlAlchemy (sqlalchemy.exc.OperationalError)

I’m learning sqlAlchemy and I want to establish a connection and create a database to insert data. I have an error message that I don’t understand.
According to the documentation, the error may come from the database itself. Has anyone ever encountered this problem?

  File "C:\Users\A.NAITSAIDI\AppData\Local\Programs\Python\Python310\lib\site- 
  packages\psycopg2\", line 122, in connect
  conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
  sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) 
  (Background on this error at:

My code :

    from flask import Flask, render_template, request
    from flask_sqlalchemy import SQLAlchemy
    app = Flask(__name__)
    #configurer la connexion a la bdd
    app.config["SQLALCHEMY_DATABASE_URI"] = 'postgresql://postgres:password@localhost:5432/data_collector'
    db= SQLAlchemy(app)

class Data(db.Model):
    __tablename__ = "data"
    id=db.Column(db.Integer, primary_key=True)
    email_=db.Column(db.String(120), unique=True)

    def __init__(self, email_, height_):

def index():
    return render_template("index.html")

@app.route("/success", methods=['POST'])
def success():

    if request.method == 'POST':
        email = request.form["email_name"]
        height = request.form["height_name"]
        return render_template("success.html")

if __name__ == "__main__":

    app.debug == True

>Solution :

Seems like database data_collector is not created on your machine. After creating the DB, you can also include this in the app file:

if __name__ == "__main__":
    app.debug == True

Leave a Reply