DocumentDB Gateway
pg addon install documentdb runs the official DocumentDB gateway — the Rust service that speaks the MongoDB wire protocol in front of the DocumentDB PostgreSQL extension — as a managed container.
Install the extension first. The gateway refuses to start unless the target instance is running, exports
/var/run/postgresql, and has all four extensions. See DocumentDB extension.
Local instances only. The official gateway
.debsupports passwordless authentication over a Unix socket and refusesPGPASSWORD, passwords in the URL, andPostgresDataUserPassword. pgcli therefore connects the gateway through a shared socket volume and rejects--dsn/--admin-dsn. A remote PostgreSQL cannot be used as the target.
Access model. The gateway connects to PostgreSQL as the dedicated role
docdb_gateway(LOGIN SUPERUSER, no password) throughlocal … trustinpg_hba.conf. MongoDB clients authenticate as a DocumentDB user (defaultdocdb_admin) with the built-in rolesreadWriteAnyDatabase+clusterAdmin— broad by design. Use a distinct--usernameper application if you need finer separation later; v1 does not expose per-collection roles.
No telemetry. pgcli does not use the upstream all-in-one image or its
usage_telemetry.sh. It runs the vendor gateway binary directly and rendersSetupConfiguration.jsonwithTelemetryOptions.Metrics.EnabledandTracing.Enabledset tofalse; no OTLP endpoint is injected.
Parameters
| Flag | Default | Description |
|---|---|---|
--name |
documentdb |
Add-on name (map key and container suffix) |
--pg-name |
— | Required. Target pgcli instance (must be PG 18, standalone, with the DocumentDB extension) |
--database |
target instance’s database | Database containing the DocumentDB extension |
--username |
docdb_admin |
MongoDB client user, created through documentdb_api.create_user. Prefixes documentdb, citus, pg, internal_role are rejected |
--password |
generated | Client password. Printed once when generated; retrieve later with pg addon password documentdb |
--tls-mode |
allow |
allow = plaintext and TLS; require = TLS only |
--image |
ghcr.io/mars-base/pgcli/pgcli-documentdb-gateway:1.0.0 |
Gateway image override |
--port |
auto (base 10260) | MongoDB wire-protocol port |
--listen |
0.0.0.0 |
Bind address (0.0.0.0, 127.0.0.1 or ::1) |
--force |
false | Recreate even when running, applying changed flags |
--dsn and --admin-dsn are rejected for this add-on.
Install
The install prints the one-time password and the endpoint:
During install pgcli also:
- Prepares the target — ensures
docdb_gatewayexists withLOGIN SUPERUSER, and adds a managedlocal all docdb_gateway trustrule at the top ofpg_hba.confonly if no matching local trust rule is present (pgcli’s default HBA already has one). - Creates or rotates the client user through
documentdb_api.create_user/update_user. The JSON document travels overpsqlstdin — never argv or PostgreSQL logs — afterSET log_statement='none'. - Renders
pg-urlandSetupConfiguration.jsonunder<base-dir>/addon/documentdb/<name>/and mounts the target’s socket volume read-only. - Starts the gateway container with
--restart unless-stopped.
Re-running the same install while the container is running is a no-op unless --force is passed.
Client Test with Python (uv + pymongo)
pgcli’s repository uses uv for ad-hoc Python dependencies. This is the end-to-end connection and CRUD test used during development:
Run it on the host where the add-on listens; from another machine set MONGO_HOST to the gateway host. TLS is off by default in the script: run with MONGO_TLS=true to enable it. This works in both allow and require modes (plaintext is rejected in require anyway). tlsAllowInvalidCertificates is needed only because the auto-generated certificate is self-signed; with a certificate you trust, use tlsCAFile="/path/to/ca.pem" instead.
Management CLI (mongosh)
pgcli can connect to and inspect a managed or remote gateway with mongosh without installing it on the host and without adding a MongoDB driver to the pgcli binary. The client runs from a separate mongosh-only image — the official mongosh 2.14.1 binary on debian:13-slim, no MongoDB server — which is pulled on first use and runs as a throwaway container. The pgcli binary does not grow.
All subcommands accept the same persistent flags:
| Flag | Default | Description |
|---|---|---|
--name |
documentdb |
Gateway add-on to connect to (explicit --name forces local mode and ignores DOCDB_URI) |
--dsn |
$DOCDB_URI |
Full MongoDB connection string — recommended for a gateway on another host |
--uri |
— | Alias for --dsn |
--host / --port |
— / 10260 |
Alternative to --dsn |
--username / --password |
$DOCDB_USERNAME / $DOCDB_PASSWORD |
Credentials with --host; prefer the environment over --password, which is visible in ps |
--tls |
false | Force TLS. Enabled automatically when a managed gateway’s --tls-mode is require |
--image |
ghcr.io/mars-base/pgcli/pgcli-mongosh:2.14.1 |
Client image override (private registry or offline) |
shell opens an interactive session; --database selects the initial database (the gateway’s target database or the URI’s database by default), and arguments after -- are forwarded to mongosh verbatim. databases list, collections list, and indexes list run read-only queries and print one name (or one JSON index document) per line; indexes list requires --collection.
Remote Gateways
No local add-on entry is needed to drive a gateway on another host. Export the connection string once so the commands stay short:
Use --dsn (or its alias --uri) for one-off commands:
Rules:
- A password missing from the URI is filled from
DOCDB_PASSWORD; missing userinfo is filled fromDOCDB_USERNAME. Prefer these environment variables over--password, whose value appears inps. - pgcli adds
authSource=admin,directConnection=true, andserverSelectionTimeoutMS=10000when the URI omits them. Values already in the URI, including its database path, win. --tlsforces TLS by addingtls=true&tlsAllowInvalidCertificates=true. Use it with a gateway inrequiremode, or put the TLS parameters in the URI yourself.--dsn/--uricannot be combined with--host/--port/--username/--password, and--namecannot be combined with any remote flag. Explicit remote flags take precedence overDOCDB_URI.--host HOST --port PORT --username USERremains available as an alternative; the password comes fromDOCDB_PASSWORD.- The client host needs podman and the mongosh image, pulled from GHCR on first use (
--imageoverrides it). The gateway must listen on an address reachable from the client: the default--listen 0.0.0.0works;127.0.0.1restricts it to the gateway host.
Remote mode only changes where the mongosh client connects — the gateway itself still targets a local PostgreSQL instance on its own host.
Common mongosh Operations
shell forwards everything after -- to mongosh. Everyday commands:
Run a single command without entering the shell:
find()shows the first 20 documents in an interactive session; typeitfor more, or.forEach(printjson)to print all._idis unique per collection, indexed automatically, and generated when omitted. Queries on_iddo not need another index.drop()anddropDatabase()delete data irreversibly. The read-only CLI helpers (databases list,collections list,indexes list) never write; useshellfor document queries and writes.- DocumentDB does not implement every MongoDB command or query operator; check the upstream compatibility notes when a feature is missing.
Notes:
- The connection string is built from the add-on’s
pg.yamlentry (client user, password, port, TLS mode) in local mode, or from--dsn/DOCDB_URIin remote mode. The password reaches the container through an environment variable passed by name only, so it does not appear in podman’s argv (podman inspect’sConfig.Cmd). - DocumentDB reports only databases that contain collections. When a list is empty, pgcli prints an explanatory note on stderr and leaves stdout empty, so scripts can keep treating stdout as data.
- The inspection subcommands are intentionally read-only. Create/drop database, collection, index, and user operations are not exposed yet; use
shell(or a MongoDB driver) for writes. - The image sets mongosh’s telemetry opt-out (
/home/mongosh/.mongodb/mongosh/config,{"enableTelemetry":false}). mongosh 2.14.1 may still print its telemetry notice at startup.
TLS
The daemon always generates a self-signed certificate on first start and persists it in the named volume pgcli-documentdb-tls-<name>, so the certificate survives container recreation.
--tls-mode allow(default): the same port accepts plaintext and TLS.--tls-mode require:EnforceTls=trueinSetupConfiguration.json; plaintext handshakes are rejected.
Switch modes with a recreate:
With require, enable TLS on the client as in the test code (MONGO_TLS=true), or use tlsCAFile when you want certificate validation.
Lifecycle
remove deletes the container; --clean-data also deletes the TLS state volume and the rendered config directory. PostgreSQL-side state is kept: DocumentDB data, the client user, the docdb_gateway role and the HBA rule survive removal. The command prints the DROP ROLE statements if you want to clean them up manually.
Autostart is supported; the gateway starts after the instances in pg start --autostart and verifies the target instance is up first:
Multiple Gateways
Each gateway gets its own name, container, port and TLS volume:
Use a distinct
--usernamewhen several gateways share the same target database. DocumentDB users are PostgreSQL roles, so a second install with the same username rotates that role’s password — invalidating the credential stored by the first gateway.
Impact on the Shared PostgreSQL Instance
Multiple gateways may target one instance, but they are not isolated from it or from each other:
| Area | Impact |
|---|---|
| Connections | Each gateway holds at least one client backend as docdb_gateway (one idle connection per gateway was observed on a running install). Under load the gateway may open more. Count the total against max_connections; v1 has no pooling layer between the gateway and PostgreSQL. |
| CPU / memory / I/O / locks | Every gateway’s queries, writes and foreground index builds (indexBuildsScheduledOnBgWorker=false) compete on the same instance. Adding gateways adds load, not capacity. |
| Data visibility | A distinct --username separates credentials, not data. Each DocumentDB user is created with readWriteAnyDatabase + clusterAdmin by default, so all gateways pointing at the same database read and write the same collections. Use separate databases or separate instances when applications need isolation; v1 exposes no per-collection roles. |
| Roles | PostgreSQL roles are cluster-wide. Client usernames must therefore be unique across gateways. pg addon remove keeps each gateway’s client role (drop it manually), while the shared docdb_gateway role must remain for the surviving gateways. |
Background workers / pg_cron |
Configured once per instance, not per gateway. Extra gateways do not start duplicate workers or cron jobs. |
Monitor actual usage with:
Limitations
- Local PG 18 instances only; standalone (not Patroni/HA);
--dsnis unsupported. (Thepg addon documentdb --dsnclient flag is unrelated — it points mongosh at a gateway running elsewhere.) - macOS is code-complete but untested.
- TLS uses the auto-generated self-signed certificate only; custom certificates are not exposed yet.
pg destroydeletes the instance’s socket volume. Remove the gateway before destroying its target instance.- The gateway connects as a superuser over local trust; the security boundary is the host/container boundary. Keep
--listen 127.0.0.1when clients are local.
Troubleshooting
Expected on a standalone gateway: no dynamic host-configuration file is configured. It is logged once at startup and does not affect operation.
If the gateway cannot connect, verify the target mount and extensions: