# Try it with Pigsty

> Deploy PostgreSQL 18 with DocumentDB and a stateless FerretDB gateway using Pigsty's mongo template.

---

LLMS index: [llms.txt](/llms.txt)

---

This example uses the [Pigsty `mongo` template](https://pigsty.io/docs/conf/mongo/) and FerretDB from the earlier PG18 study. The configuration was `conf/mongo.yml` in Pigsty v4.5.0 and `conf/fork/mongo.yml` in its development sources at the time. [Single-node Mimus 19.0.1 results](/docs/concepts/architecture/#documentdb)

## Single-node deployment {#single-node}

The default `mongo` template deploys PostgreSQL 18, the `documentdb` extension stack and a local FerretDB container on a single node (`10.10.10.10`):

1. Configure and deploy the PostgreSQL cluster, then install Docker and launch the `ferretdb` application container on the `pg-meta` node:

   ```bash
   ./configure -c mongo
   ./deploy.yml
   ./docker.yml -l pg-meta
   ./app.yml -l pg-meta
   ```

2. Connect from `mongosh` or any MongoDB driver to the local loopback listener on port `27017` using the backend PostgreSQL user created by the template:

   ```bash
   mongosh 'mongodb://mongod:<password>@127.0.0.1:27017/'
   ```
{.steps}

## What the template configures {#configuration}

On the PostgreSQL side (`pg_version: 18`), the `mongo` template declares a `mongod` backend superuser, sets `cron.database_name` to `postgres`, preloads the DocumentDB and `pg_cron` libraries, and installs the required extensions in the `postgres` database:

```yaml
pg_version: 18
pg_users:
  - { name: mongod, password: <password>, superuser: true }
pg_databases:
  - { name: postgres, extensions: [ documentdb, postgis, vector, pg_cron, rum ] }
pg_parameters:
  cron.database_name: postgres
pg_extensions: [ documentdb, postgis, pgvector, pg_cron, rum ]
pg_libs: 'pg_documentdb, pg_documentdb_core, pg_documentdb_extended_rum, pg_cron, pg_stat_statements, auto_explain'
```

On the gateway side, Pigsty deploys FerretDB (`ghcr.io/ferretdb/ferretdb:2.7.0`) as a stateless Docker application under `apps.ferretdb.conf`. The container connects to Pigsty's local primary-routing service on port `5436` via `host.docker.internal`:

```yaml
docker_enabled: true
app: ferretdb
apps:
  ferretdb:
    conf:
      FERRETDB_IMAGE: ghcr.io/ferretdb/ferretdb:2.7.0
      FERRETDB_POSTGRESQL_URL: 'postgres://mongod:<password>@host.docker.internal:5436/postgres?pool_min_conns=1&pool_max_conns=20'
      FERRETDB_BIND_ADDR: 127.0.0.1
      FERRETDB_PORT: 27017
      FERRETDB_LISTEN_ADDR: ':27017'
      FERRETDB_AUTH: true
      FERRETDB_TELEMETRY: disabled
```

## Three-node high-availability topology {#ha-topology}

Because FerretDB holds no state, high availability uses the standard PostgreSQL Patroni + etcd cluster layout (`pg-mongo` in the template):

1. Each of the three PostgreSQL nodes (`10.10.10.11`–`10.10.10.13`) runs a local FerretDB container bound to `{{ inventory_hostname }}:27018` and connected to its local port `5436` primary-routing service.
2. HAProxy runs TCP health checks (`option tcp-check` on port `27018`) across the three gateway containers and exposes a single floating endpoint at `10.10.10.4:27017` (`mongo.pigsty`).
3. Patroni and etcd handle PostgreSQL primary failover, while pgBackRest writes backups to a shared [Silo](https://silo.pgsty.com/) or external S3 repository so backup history survives primary switchovers.

After uncommenting the `pg-mongo` cluster and the two additional etcd members in the template, deploy with:

```bash
./configure -c fork/mongo
./deploy.yml
./docker.yml -l pg-mongo
./app.yml -l pg-mongo
mongosh 'mongodb://mongod:<password>@10.10.10.4:27017/'
```

## Backup scope for DocumentDB 1.2 {#backup}

On PostgreSQL 19 with DocumentDB 1.2 and pg_cron 1.6.8, exclude the cron schema at dump time only if the destination may recreate the default jobs and you can discard user jobs, schedule or active-state changes, original job IDs, and execution history.

```sh
pg_dump -Fc -N cron -d doclab -f doclab.dump
```

This omits the entire cron schema, including jobs registered by other extensions. To keep your options open, omit `-N cron` at dump time and retain the full custom-format archive. Use `pg_restore --exit-on-error -N cron -d doclab doclab.dump` only on a fresh destination that may start with default jobs. Other restores can still use the original scheduling data. If scheduling state must survive, keep it in the backup and use a tested restore procedure. Excluding only `cron.job` leaves history pointing to old job IDs.

A PostgreSQL logical archive of stock DocumentDB 1.2 can contain the physical document tables but omit the catalog that maps collection names to those tables. A successful `pg_restore` can therefore leave a collection returning zero documents. To preserve the full document database, use a tested same-version physical backup. On a separate restored instance, read known documents by collection name and check their indexes. Include `documentdb_data` whenever the backup is intended to preserve documents.

## Operational notes {#notes}

- **Loopback binding by default**: in single-node mode, `FERRETDB_BIND_ADDR` defaults to `127.0.0.1`. Change `FERRETDB_BIND_ADDR` only when remote MongoDB clients need direct network access.
- **PostgreSQL is the security boundary**: `FERRETDB_AUTH: true` authenticates clients against PostgreSQL roles, but FerretDB does not implement MongoDB role-based authorization. Tighten `pg_hba.conf` and change default passwords before production use.
- **Client TLS**: the template does not enable client-side MongoDB TLS by default; configure certificates and the `FERRETDB_LISTEN_TLS*` variables before exposing port `27017` on an untrusted network.
- **Upgrade smoke tests**: FerretDB releases specify a preferred DocumentDB version while package repositories may ship a newer compatible `documentdb` build. Run an authenticated CRUD smoke test after upgrading either component.
