Skip to content

Postgres

OpenGateLLM uses PostgreSQL as its primary relational database. It has two main purposes:

  • Ressources reference: store and manage the application resources (roles, organizations, users, permissions, api keys, models, collections, documents)
  • Usage monitoring: track API requests with metrics (tokens, costs, duration, carbon footprint…)

Check the schema of the database in the models.py file.

  • Directoryapi
    • Directorysql
      • models.py SQLAlchemy models
      • session.py

To configure the PostgreSQL dependency, you need to add the database connection information in the dependencies section of your config.yml file.
Check Postgres section in configuration file documentation for more information.

Example:

dependencies:
[...]
postgres:
url: postgresql+asyncpg://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD:-changeme}@${POSTGRES_HOST:-localhost}:${POSTGRES_PORT:-5432}/postgres
echo: false
pool_size: 5
connect_args:
server_settings:
statement_timeout: "120s"
command_timeout: 60

The PostgreSQL dependency accepts all parameters from the SQLAlchemy AsyncEngine. Only url parameter is required. The connection URL must use the postgresql+asyncpg:// driver. If you provide a standard postgresql:// URL, it will be automatically converted to use asyncpg.

Configuration file documentation

OpenGateLLM uses Alembic for database migrations. The API image contains them, but the API does not run them when it starts: a deployment runs them once, with the same image, before starting the API containers. Several API containers starting together would otherwise race on the migration, and a failing migration would keep each of them restarting in a loop.

With Docker Compose, the db-migrate service of compose.example.yml does it: the api service waits for it, and does not start if it fails. It also publishes the OpenFGA authorization model and runs the OpenFGA data migrations, so it needs OpenFGA to be up.

To upgrade a running instance, run the migrations as their own step:

Terminal window
docker compose pull && docker compose run --rm db-migrate && docker compose up --detach --wait

A single docker compose up replaces the running api container before the migrations end: if they fail, the new container stays stopped and the API is down. With docker compose run, a failure leaves the running API untouched.

Without Docker Compose, run this command before starting the API containers:

Terminal window
docker run --rm --network <network of postgres and openfga> -v ./config.yml:/config.yml:ro ghcr.io/etalab-ia/opengatellm/api:<version> \
bash -c "python -m alembic -c api/alembic.ini upgrade head && bash scripts/setup_openfga.sh && python -m api.infrastructure.openfga.migrations"

OpenFGA stores who can act on which resource. When a release starts reading a new relation, the rows created before it do not hold that relation yet: an OpenFGA data migration reads them from PostgreSQL and writes the missing relations to OpenFGA. The migrations ship in the API image and db-migrate runs them after the Alembic upgrade.

They are idempotent and never delete anything: each relation is checked before it is written, so running them again writes nothing (granted=0). To see what they would write without writing it:

Terminal window
docker compose run --rm db-migrate python -m api.infrastructure.openfga.migrations --dry-run

--only <migration> runs a single one, m001_platform_admins for instance.

For contributing to the database schema, you can follow the instructions in SQL contributions guide.