﻿<head><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-4357510538660394"
     crossorigin="anonymous"></script></head>---
url: https://docs.puppetflow.com/self-hosting/database/postgresql.md
---
# PostgreSQL

PostgreSQL is the default database for the production Compose stack and the recommended backend for horizontal scaling or deployments with application and worker processes on multiple hosts.

## Bundled PostgreSQL

The production Docker Compose stack includes PostgreSQL 17 and stores its data in the persistent `puppetflow-db` volume.

Configure the shared `.env` file:

```dotenv
DB_CONNECTION=pgsql
DB_HOST=postgres
DB_PORT=5432
DB_DATABASE=puppetflow
DB_USERNAME=puppetflow
DB_PASSWORD=
DB_SSLMODE=prefer
```

Set a strong, unique `DB_PASSWORD`. The `DB_DATABASE`, `DB_USERNAME`, and `DB_PASSWORD` values are also used to initialize the bundled PostgreSQL container.

Start the stack and apply migrations:

```bash
docker compose up -d
docker compose exec app php artisan migrate --force
```

## External PostgreSQL

For a managed or external PostgreSQL service:

```dotenv
DB_CONNECTION=pgsql
DB_HOST=postgres.example.internal
DB_PORT=5432
DB_DATABASE=puppetflow
DB_USERNAME=puppetflow
DB_PASSWORD=
DB_SSLMODE=require
```

Allow connections from every Puppetflow application and worker host. The database user must be able to create and alter tables, indexes, foreign keys, and check constraints when migrations run.

When the external service replaces the bundled database:

1. Remove the `postgres` service from the customized Compose file.
2. Remove `depends_on: postgres` from `app`, `queue-1`, `queue-2`, `scheduler`, and `mailbox-smtp`.
3. Remove the unused `puppetflow-db` volume.
4. Recreate the Puppetflow services.

## Queue, cache, and sessions

The recommended configuration keeps application state in PostgreSQL:

```dotenv
QUEUE_CONNECTION=database
DB_QUEUE_RETRY_AFTER=900
CACHE_STORE=database
SESSION_DRIVER=database
```

This configuration keeps queued jobs, cache entries, distributed locks, and sessions durable. Use Redis for these features only when connecting to a separate Redis deployment with persistence and backups.

## Backups

Back up PostgreSQL independently from S3, R2, or local file storage. A basic logical backup is:

```bash
docker compose exec -T postgres pg_dump \
  --username=puppetflow \
  --dbname=puppetflow \
  --format=custom > puppetflow.dump
```

Restore into an empty database:

```bash
cat puppetflow.dump | docker compose exec -T postgres pg_restore \
  --username=puppetflow \
  --dbname=puppetflow \
  --clean \
  --if-exists
```

Test restore procedures regularly. Managed PostgreSQL services should also use automated snapshots and point-in-time recovery when available.

## Operational checklist

* Monitor free disk space, connections, slow queries, and lock waits.
* Keep automated backups outside the database host.
*