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\__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError)
(Background on this error at: https://sqlalche.me/e/14/e3q8)
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)
height_=db.Column(db.Integer)
def __init__(self, email_, height_):
self.email_=email_
self.height_=height_
@app.route("/")
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"]
print(email)
print(height)
return render_template("success.html")
if __name__ == "__main__":
app.debug == True
app.run()
>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__":
db.create_all()
app.debug == True
app.run()