-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
enh: more mature support for external (non-SQLite) databases #2660
Comments
Great suggestion! As long as it won't break the existing default sqlite db, PR welcome! |
Is this the reason why I get 404 not found errors after a certain period of time after initial startup, when using postgresql? Works fine on restart but logins always fail after some time. |
@devdev999 IDK, probably not, my observation is that when connection to postgres is no longer alive - login fails with 500 status code with something like "connection already closed" in body. (But it depends on how openwebui is deployed in your case, maybe something between your browser and OpenWebUI is converting these 500s to 404) You can increase log verbosity by setting following env variables (main one here is UVICORN_LOG_LEVEL, and OpenWebUI-specific you can find here):
|
Is your feature request related to a problem? Please describe.
Currently, there is support for specifying an external database via the
DATABASE_URL
environment variable instead of the default SQLite (added in #1397).However, it seems that OpenWebUI (at least in the
main
branch) doesn't fully manage the database connection lifecycle, likely because SQLite is the default option, and connection management is often less critical in this context."Standard" approach to handling database connections in web applications looks something like
Init database connection pool
Start web server (so (1) should be done via something like lifespan in FastAPI terms)
On every incoming request - take connection from pool, execute required sql, return response to the request - and return connection to pool
Before application shutdown - gracefully close all connections to database
When using SQLite, these steps (outlined in the collapsible section) may seem overcomplicated, so OpenWebUI currently creates a single connection to the database at application start.
However, if we specify a Postgresql DSN via the
DATABASE_URL
env var, OpenWebUI works fine only for a limited time.The single connection will eventually be closed by the database since connections are not immortal. Currently, neither Peewee nor OpenWebUI handles reconnections in such scenarios.
Describe the solution you'd like
OpenWebUI is build using FastAPI framework and uses Peewee ORM.
FastAPI documentation includes tutorial / how-to work with Peewee from asynchronous application, and Peewee documentation points to the same tutorial.
In short - this tutorial is about ensuring that
First part can be achieved by adding the following code to the
backend/apps/web/internal/db.py
file (adapted from the tutorial linked above):The second part is about ensuring an active connection. This can be done using following ad-hoc method:
Peewee has an almost ready-made solution: mixin playhouse.shortcuts.ReconnectMixin.
We just need to adapt this to psycopg2 exceptions (in the same
backend/apps/web/internal/db.py
file):I acknowledge that this approach may not be ideal, but it should be an improvement.
Describe alternatives you've considered
Migrating from Peewee ORM to SQLAlchemy ORM (or SQLAlchemy Core) could greatly benefit the project. However, this requires substantial refactoring, which means a significant investment of person-hours to implement and even more time to properly review and test the changes.
Additional context
Q&A:
Any feedback or guidance on this would be greatly appreciated. Thank you for your hard work and dedication to this project.
The text was updated successfully, but these errors were encountered: