Skip to content
AI GatewayAI Gateway Manual

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.

These three .env keys must be changed together:

KeyBuilt-in serviceExternal server
DATABASE_URLEmptypostgres://<user>:<password>@<host>:5432/<database>?sslmode=require
REDIS_URLEmptyrediss://:<password>@<host>:6379/0
COMPOSE_PROFILESpostgres,redisRemove 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_PROFILES

When 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.

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.

Percent-encode any special character in the user name or password:

CharacterEncoded 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)

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.

Pass the URLs as environment variables before running ./install.sh install. Use read -rs to avoid leaving passwords in the shell history:

Terminal window
# PostgreSQL only:
read -rs -p 'DATABASE_URL: ' AIG_DATABASE_URL; echo
export AIG_DATABASE_URL
# Redis only:
# read -rs -p 'REDIS_URL: ' AIG_REDIS_URL; echo
# export AIG_REDIS_URL
./install.sh install --hostname aig.example.com

Uncomment 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:

Terminal window
cd aig/current
docker compose ps -a

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:

Terminal window
cd aig/current
./install.sh install

Every 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:

Terminal window
cd aig/current
docker compose stop server web

Dump the database using the bundled pg_dump. The dump uses custom format (-Fc) for reliable restoration regardless of locale settings:

Terminal window
docker compose exec -T postgres pg_dump -U ai_gateway -d ai_gateway -Fc > aig.dump

Restore 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:

Terminal window
docker compose exec -T postgres pg_restore --no-owner --no-privileges \
-d 'postgres://<user>:<password>@<external-host>:5432/<database>?sslmode=require' < aig.dump

Verify the restore completed without errors before continuing. Then update .env:

DATABASE_URL=postgres://<user>:<password>@<external-host>:5432/<database>?sslmode=require
COMPOSE_PROFILES=redis

Restart services and confirm the application is healthy:

Terminal window
./install.sh install
curl -sf http://127.0.0.1/readyz && echo ok

Remove the bundled PostgreSQL container:

Terminal window
docker compose --profile postgres rm -sf postgres

The data volume aig_pgdata remains on disk. After confirming the external database is working, remove it:

Terminal window
docker volume rm aig_pgdata

install.sh upgrade cannot dump an external PostgreSQL. Back up the external database with your own tools before upgrading, then pass --no-backup:

Terminal window
./install.sh upgrade 2026.11.15 --no-backup

Running 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 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.