Search code examples
postgresqlflaskflask-sqlalchemypsycopg2flask-session

How to set the PostgreSQL database's schema name in a Flask session


With flask session, I was able to successfully create a sessions table, but I now need to set the schema name in the PostgreSQL database.

How do I set it up?

def create_app():
    app = Flask(__name__)

    cors = CORS(app, resources={r"/*": {"origins": "http://localhost:8080/"}})
    app.config['SESSION_TYPE'] = 'sqlalchemy'
    app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:password@localhost:5432/database'
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
    app.config["SESSION_SQLALCHEMY_TABLE"] = "sessions" #default sessions
    app.config["SESSION_COOKIE_SECURE"] = True
    app.config["PERMANENT_SESSION_LIFETIME"] = timedelta(seconds=50)
    session = Session(app)
    session.app.session_interface.db.create_all()


    return app

Solution

  • Resolved, by adding the options parameter to the db url.

    postgresql://postgres:password@localhost:5432/database?options=-c%20search_path=schema_name