how to connect my dockerized postgres database to my app

Viewed 17

Hi i'm trying to follow this tutorial https://www.digitalocean.com/community/tutorials/how-to-add-authentication-to-your-app-with-flask-login-fr but i can't figure out how to add my postgres database connection instead of the sqlite they are using.

I tried to do this =>

from flask import Flask
from flask_login import LoginManager
import os
import psycopg2

db = psycopg2.connect(
      host="localhost",
      database="postgres",
      user=os.environ['DB8USERNAME'],
      password=os.environ['DB_PASSWORD'])

def create_app():
    app = Flask(__name__)
    
    db.commit()
    
    login_manager = LoginManager()
    login_manager.login_view = 'auth.login'
    login_manager.init_app(app)
    
    from .models import User
    
    @login_manager.user_loader
    def load_user(user_id):
        return User.query.get(int(user_id))
    
    from .auth import auth as auth_blueprint
    app.register_blueprint(auth_blueprint)
    
    from.main import main as main_blueprint
    app.register_blueprint(main_blueprint)
    
    db.close()
    return app

but i get this error =>

enter image description here

I wrote a script that creates a table in my database so i know that my dockerized postgres image works well and is well attached to pgadmin.

But i'm stuck on how i can link my database to my app. Is there a way to just link it in a uri as in the tutorial?

0 Answers
Related