Postgres
OpenGateLLM uses PostgreSQL as its primary relational database to manage application resources and track API usage.
PostgreSQL is a required dependency for OpenGateLLM to function.
Overview
PostgreSQL stores and manages:
- Identity & Access Management (IAM): Users, roles, permissions, tokens, and organizations
- Usage Logs: API call tracking with metrics (tokens, costs, duration, carbon footprint)
- Document Management: Collections and documents for RAG functionality
- Model Registry: Model configurations, providers, and aliases
The database includes the following main tables:
Identity & Access Management
user: User accounts with authentication informationrole: User roles for permission managementpermission: Fine-grained permissions assigned to rolestoken: API tokens for authenticationorganization: Organizations for grouping users
Usage Tracking
usage: Comprehensive API usage logs including:- Request metadata (endpoint, method, model)
- Token consumption (prompt, completion, total)
- Performance metrics (duration, time to first token)
- Cost tracking
- Environmental impact (kWh, CO₂ emissions)
Document Management
collection: Document collections for RAGdocument: Documents stored in collections
Model Registry
model: Model routers and configurationsmodel_client: Model provider clientsmodel_alias: Model aliases for routing
Setup PostgreSQL
Prerequisites
PostgreSQL 16 or higher is recommended.
Configuration
Docker Compose
Add a postgres container in the services section of your compose.yml file:
services:
[...]
postgres:
image: postgres:16.5
restart: always
user: postgres
environment:
- "POSTGRES_USER=${POSTGRES_USER:-postgres}"
- "POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-changeme}"
- "POSTGRES_DB=postgres"
ports:
- "${POSTGRES_PORT:-5432}:5432"
volumes:
- postgres:/var/lib/postgresql/data
healthcheck:
test: [ "CMD-SHELL", "pg_isready", "-U", "postgres" ]
interval: 4s
timeout: 10s
retries: 5
start_period: 60s
volumes:
postgres:
Configuration File
For more information about the configuration file, see Configuration documentation.
-
Add PostgreSQL configuration in the
dependenciessection of yourconfig.yml. 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: 60The PostgreSQL dependency accepts all parameters from the SQLAlchemy AsyncEngine. Only
urlparameter is required.warningThe connection URL must use the
postgresql+asyncpg://driver. If you provide a standardpostgresql://URL, it will be automatically converted to use asyncpg.-
For usage monitoring, set
monitoring_postgres_enabledtotruein settings (enabled by default).settings:
[...]
monitoring_postgres_enabled: trueAll requests to the API are logged in the
usagetable. For more information about usage tracking, see Usage monitoring documentation.
-
Database Migrations
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 documentation.
Data Retention
For production deployments, consider implementing a data retention policy for the usage table to manage database size.