# Self-host Keycloak (Postgres)

> Keycloak backed by Postgres instead of its default embedded database, for a real multi-instance setup.

- Page: https://levelrail.com/templates/keycloak-postgres/
- JSON: https://levelrail.com/templates/keycloak-postgres.json
- Category: Security
- Licence: Apache-2.0 (Permissive license)
- Source: https://github.com/keycloak/keycloak
- Upstream docs: https://www.keycloak.org/documentation

## Requirements
- Memory: about 1 GiB
- amd64: published, arm64: published
- Needs configuration before start: no
- Pinned image tag: 16-alpine

## Services

| Service | Image | Tag | Ports | Volumes |
| --- | --- | --- | --- | --- |
| db | postgres | 16-alpine | none | keycloak_postgres_db_data -> /var/lib/postgresql/data |
| keycloak | quay.io/keycloak/keycloak | 26.1 | 8080 | keycloak_postgres_data -> /opt/keycloak/data |

## Environment

| Service | Variable | Kind |
| --- | --- | --- |
| db | POSTGRES_DB | preset |
| db | POSTGRES_PASSWORD | generated |
| db | POSTGRES_USER | preset |
| keycloak | KC_BOOTSTRAP_ADMIN_PASSWORD | generated |
| keycloak | KC_BOOTSTRAP_ADMIN_USERNAME | generated |
| keycloak | KC_DB | preset |
| keycloak | KC_DB_PASSWORD | generated |
| keycloak | KC_DB_URL | preset |
| keycloak | KC_DB_USERNAME | preset |
| keycloak | KC_HEALTH_ENABLED | preset |
| keycloak | KC_HOSTNAME | preset |
| keycloak | KC_HTTP_ENABLED | preset |
| keycloak | KC_LEGACY_OBSERVABILITY_INTERFACE | preset |
| keycloak | KC_PROXY_HEADERS | preset |

## Deploy

### CLI

```bash
levelrail-cli templates deploy keycloak-postgres --name my-keycloak-postgres
```

### API

```bash
curl -X POST -H "Authorization: Bearer $TOKEN" \
  https://levelrail.example.com/api/v1/service-templates/keycloak-postgres/deploy
```

Create a token in the dashboard first. This one-click route deploys it as a trial app you can keep or remove.

## Questions

### Can I deploy Keycloak (Postgres) on Dokploy or Dokku?

The services and pinned images on this page can be run on any Docker host. The public Dokploy templates list we compared does not have an entry with the same id for Keycloak (Postgres), so on Dokploy you would use a Docker Compose application with the file from this page. Dokku deploys container images with its own commands, so the pinned image postgres:16-alpine, quay.io/keycloak/keycloak:26.1 is what you deploy there.

Self-host Keycloak (Postgres), Keycloak (Postgres) docker compose file, deploy Keycloak (Postgres) on a VPS: this page lists the pinned image, ports, volumes and the commands for each.
