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 schema is automatically initialized and updated when the API starts by startup_api.sh script.

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