# Self-host PostgreSQL

> The PostgreSQL relational database server on the official Alpine image, with a persistent volume and a generated password. It is not published on the host, so reach it from other services in this project at postgres:5432.

- Page: https://levelrail.com/templates/postgres/
- JSON: https://levelrail.com/templates/postgres.json
- Category: Database Tools
- Licence: PostgreSQL (Permissive license)
- Source: https://github.com/postgres/postgres
- Upstream docs: https://www.postgresql.org/docs/17/

## Requirements
- Memory: about 256 MiB
- amd64: unknown, arm64: unknown
- Needs configuration before start: no
- Pinned image tag: 17.11-alpine

## Services

| Service | Image | Tag | Ports | Volumes |
| --- | --- | --- | --- | --- |
| postgres | postgres | 17.11-alpine | 5432 | pgdata -> /var/lib/postgresql/data |

## Environment

| Service | Variable | Kind |
| --- | --- | --- |
| postgres | POSTGRES_DB | preset |
| postgres | POSTGRES_PASSWORD | generated |
| postgres | POSTGRES_USER | preset |

## Deploy

### CLI

```bash
levelrail-cli templates install postgres --name my-postgres
```

Add --domain SERVICE=HOST to serve a service on your own domain, for example --domain postgres=app.example.com.

### API

```bash
curl -X POST -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
  https://levelrail.example.com/api/v1/service-templates/postgres/install \
  -d '{
    "name": "my-postgres"
  }'
```

Create a token in the dashboard first. Name, env and domains are the request fields.

## First run
1. Read the generated password from the app secrets, then connect with psql or any client from a service in the same project.

## Questions

### Can I deploy PostgreSQL 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 PostgreSQL, 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:17.11-alpine is what you deploy there.

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