External PostgreSQL and Redis
Move AI Gateway storage off-host by setting DATABASE_URL, REDIS_URL, and COMPOSE_PROFILES, with requirements, URL encoding rules, TLS options, and migration steps.
By default, AI Gateway starts bundled PostgreSQL 16 and Redis 7 containers alongside the application containers. If your organization operates its own database or Redis cluster (for example AWS RDS or ElastiCache), you can point AI Gateway at those servers instead.
You can use an external server for one or both services independently.
The three keys
Section titled “The three keys”These three .env keys must be changed together:
| Key | Built-in service | External server |
|---|---|---|
DATABASE_URL | Empty | postgres://<user>:<password>@<host>:5432/<database>?sslmode=require |
REDIS_URL | Empty | rediss://:<password>@<host>:6379/0 |
COMPOSE_PROFILES | postgres,redis | Remove the profile of each service you moved off-host. Both external: empty value. |
COMPOSE_PROFILES controls which bundled services Docker Compose starts. When a
URL is empty, install.sh requires the corresponding service name to be present
in COMPOSE_PROFILES; if the name is absent, the script stops before starting
any container:
[aig] ERROR: DATABASE_URL is empty in .env but COMPOSE_PROFILES does not include postgres;set DATABASE_URL or add postgres to COMPOSE_PROFILESWhen a URL is set and the corresponding profile is still in COMPOSE_PROFILES,
the script continues but logs a warning that the bundled service is running
unused.
Requirements
Section titled “Requirements”PostgreSQL 16 or later. Provide one empty database and an account with
permission to create tables in its public schema (database owner is
recommended). AI Gateway does not use extensions. The migrate service runs
schema migrations on first install and on every upgrade using this account.
Redis 7, single endpoint. Redis Cluster and Redis Sentinel are not
supported. For ElastiCache, use a cluster-mode-disabled replication group and
the primary endpoint. The server must permit Lua scripting (EVAL).
The migrate and server containers must be able to reach both servers from
the host network. The web container does not connect to either datastore.
URL encoding
Section titled “URL encoding”Percent-encode any special character in the user name or password:
| Character | Encoded form |
|---|---|
@ | %40 |
: | %3A |
/ | %2F |
$ | %24 |
The $ encoding is critical: Docker Compose interpolates $ in .env values
as a variable reference. install.sh rejects any URL that contains $,
double-quotes, single-quotes, or whitespace:
[aig] ERROR: DATABASE_URL must not contain $, quotes, or whitespace; percent-encode them ($ is %24)TLS options
Section titled “TLS options”PostgreSQL:
sslmode=require encrypts the connection but does not verify the server
certificate. This is the recommended starting point and the example value in
env.template. To require certificate verification
(sslmode=verify-full or sslmode=verify-ca), you would also need to inject a
CA certificate into the container, which is not currently supported. There is no
way to configure a private CA bundle.
Redis:
redis://— no encryption.rediss://— TLS with verification against public certificate authorities.rediss://<host>?skip_verify=true— TLS without certificate verification, for private CA certificates.
There is no mechanism to inject a private CA certificate into the container, so
skip_verify=true is the only option when the Redis server uses a private CA.
First install with external datastores
Section titled “First install with external datastores”Pass the URLs as environment variables before running ./install.sh install.
Use read -rs to avoid leaving passwords in the shell history:
# PostgreSQL only:read -rs -p 'DATABASE_URL: ' AIG_DATABASE_URL; echoexport AIG_DATABASE_URL
# Redis only:# read -rs -p 'REDIS_URL: ' AIG_REDIS_URL; echo# export AIG_REDIS_URL
./install.sh install --hostname aig.example.comUncomment the Redis lines (or both pairs) for the services you are moving
off-host, and pass only those variables. The installer
writes the URLs and the matching COMPOSE_PROFILES into .env only when the
file does not yet exist. POSTGRES_PASSWORD and REDIS_PASSWORD are always
generated, even when the bundled services are not used, because Docker Compose
evaluates all variable references in the Compose file regardless of which
profiles are active.
After installation, the docker compose ps output should show no postgres or
redis container for a fully external configuration:
cd aig/currentdocker compose ps -aChanging datastores after installation
Section titled “Changing datastores after installation”Edit aig/current/.env directly. Change DATABASE_URL, REDIS_URL, and
COMPOSE_PROFILES together, then re-run the installer so it validates the
configuration before restarting the services:
cd aig/current./install.sh installEvery other .env value stays as it is. install.sh refuses to start when a
URL is empty but its bundled service is still listed in COMPOSE_PROFILES, or
the reverse, so this run catches a mismatched edit. Plain
docker compose up -d skips that check.
If the change includes switching the running PostgreSQL from the bundled container to an external server, migrate the data first (see below).
Migrating bundled PostgreSQL to an external server
Section titled “Migrating bundled PostgreSQL to an external server”Stop the application services but leave the bundled PostgreSQL running so you can dump from it:
cd aig/currentdocker compose stop server webDump the database using the bundled pg_dump. The dump uses custom format
(-Fc) for reliable restoration regardless of locale settings:
docker compose exec -T postgres pg_dump -U ai_gateway -d ai_gateway -Fc > aig.dumpRestore to the external server using the bundled pg_restore. Pass the full
external connection string as the target database (-d). The --no-owner and
--no-privileges flags prevent errors when the external account is not a
superuser:
docker compose exec -T postgres pg_restore --no-owner --no-privileges \ -d 'postgres://<user>:<password>@<external-host>:5432/<database>?sslmode=require' < aig.dumpVerify the restore completed without errors before continuing. Then update
.env:
DATABASE_URL=postgres://<user>:<password>@<external-host>:5432/<database>?sslmode=requireCOMPOSE_PROFILES=redisRestart services and confirm the application is healthy:
./install.sh installcurl -sf http://127.0.0.1/readyz && echo okRemove the bundled PostgreSQL container:
docker compose --profile postgres rm -sf postgresThe data volume aig_pgdata remains on disk. After confirming the external
database is working, remove it:
docker volume rm aig_pgdataUpgrading when DATABASE_URL is set
Section titled “Upgrading when DATABASE_URL is set”install.sh upgrade cannot dump an external PostgreSQL. Back up the external
database with your own tools before upgrading, then pass --no-backup:
./install.sh upgrade 2026.11.15 --no-backupRunning upgrade without --no-backup when DATABASE_URL is set is rejected:
[aig] ERROR: DATABASE_URL points to a PostgreSQL this script cannot back up;back it up with your own tools, then run './install.sh upgrade 2026.11.15 --no-backup'See Upgrade for the full procedure.
Redis data
Section titled “Redis data”Redis holds MCP session state, request rate counters, and similar runtime data. This data is not migrated when you switch to an external Redis server. Switching to a new Redis endpoint resets those counters and terminates active MCP sessions.