Home > Mobile >  How can i connect this hello-flask-app with my database and create model in the database
How can i connect this hello-flask-app with my database and create model in the database

Time:07-30

I got this error trying to create a model for my database using fask_sqlalchemy

Traceback (most recent call last): File "/usr/lib/python3/dist-packages/sqlalchemy/engine/base.py", line 3250, in _wrap_pool_connect return fn() File "/usr/lib/python3/dist-packages/sqlalchemy/pool/base.py", line 310, in connect return _ConnectionFairy._checkout(self) File "/usr/lib/python3/dist-packages/sqlalchemy/pool/base.py", line 868, in _checkout fairy = _ConnectionRecord.checkout(pool) File "/usr/lib/python3/dist-packages/sqlalchemy/pool/base.py", line 476, in checkout rec = pool.do_get() File "/usr/lib/python3/dist-packages/sqlalchemy/pool/impl.py", line 145, in do_get with util.safe_reraise(): File "/usr/lib/python3/dist-packages/sqlalchemy/util/langhelpers.py", line 70, in exit compat.raise( File "/usr/lib/python3/dist-packages/sqlalchemy/util/compat.py", line 207, in raise raise exception File "/usr/lib/python3/dist-packages/sqlalchemy/pool/impl.py", line 143, in _do_get return self._create_connection() File "/usr/lib/python3/dist-packages/sqlalchemy/pool/base.py", line 256, in _create_connection return _ConnectionRecord(self) File "/usr/lib/python3/dist-packages/sqlalchemy/pool/base.py", line 371, in init self.__connect() File "/usr/lib/python3/dist-packages/sqlalchemy/pool/base.py", line 665, in connect with util.safe_reraise(): File "/usr/lib/python3/dist-packages/sqlalchemy/util/langhelpers.py", line 70, in exit compat.raise( File "/usr/lib/python3/dist-packages/sqlalchemy/util/compat.py", line 207, in raise raise exception File "/usr/lib/python3/dist-packages/sqlalchemy/pool/base.py", line 661, in __connect self.dbapi_connection = connection = pool._invoke_creator(self) File "/usr/lib/python3/dist-packages/sqlalchemy/engine/create.py", line 590, in connect return dialect.connect(*cargs, **cparams) File "/usr/lib/python3/dist-packages/sqlalchemy/engine/default.py", line 597, in connect return self.dbapi.connect(*cargs, **cparams) File "/home/mikado/.local/lib/python3.10/site-packages/psycopg2/init.py", line 122, in connect conn = _connect(dsn, connection_factory=connection_factory, **kwasync) psycopg2.OperationalError: connection to server at "127.0.0.1", port 5432 failed: fe_sendauth: no password supplied

My Code:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy


app = Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql psycopg2://mikado:@127.0.0.1:5432/hellodb'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)

class Person(db.Model):
    __tablename__= 'persons'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(), nullable=False)
    
db.create_all()

@app.route('/')

def index():
    return 'Hello World!'

if __name__ == '__main__':
    app.run(host="0.0.0.0")

CodePudding user response:

Based on the error, you haven’t supplied any credentials for SQL:

no password supplied

You’ll need to use connection strings to pass the password as well as explained on https://flask-sqlalchemy.palletsprojects.com/en/2.x/config.html. Amending your existing connection string, replace the <password here> section with the relevant information:

postgresql psycopg2://mikado:<password here>@127.0.0.1:5432/hellodb
  • Related