> ## Documentation Index
> Fetch the complete documentation index at: /docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Connect an agent to PostgreSQL

> Let an agent use a PostgreSQL connection stored in its vault, through the OneCLI gateway, without seeing the database password.

An agent reaches a PostgreSQL connection in its vault through the gateway. The agent never receives the database password. It logs in to the gateway with its own agent credential, and the gateway logs in to the real database with the stored password.

## How the login works

The agent connects to the gateway's agent port (the same `host:port` as its proxy address, `gateway:10255` by default). A standard PostgreSQL client works:

| Startup field | Value |
| - | - |
| Host and port | The gateway's agent address |
| `user` | The ID of the PostgreSQL connection in the vault |
| `password` | The agent's credential (`oc_agent_...`) |
| `dbname` | The database on the real server |

The gateway requires TLS. A plain startup without TLS is refused, so the agent credential never crosses the network in clear text.

## Choose the TLS mode

The gateway presents a certificate signed by your instance's certificate authority, under the fixed name `postgres.gateway.onecli`. Download that CA from `/v1/gateway/ca` on your API. The connect script points `PGSSLROOTCERT` in `agent.env` at `ca-bundle.pem`, which holds the system roots and that CA.

| `sslmode` | Result |
| - | - |
| `verify-ca` | Recommended. Checks that the gateway's certificate chains to your instance's CA. |
| `require` | Works, but doesn't check the certificate. Use it only on a network you already trust. |
| `verify-full` | Fails. The certificate's name is `postgres.gateway.onecli`, not the gateway's host name, so the host check fails. |

```bash theme={null}
. "$ONECLI_HOME/agent.env"
PGPASSWORD="$ONECLI_AGENT_CREDENTIAL" psql \
  "host=gateway port=10255 user=<connection-id> dbname=app sslmode=verify-ca sslrootcert=$PGSSLROOTCERT"
```

Keep the credential in the environment, as above, rather than in the connection string, so it doesn't land in shell history or process lists.

> [!WARNING]
>
> Don't use `sslmode=verify-full` with the gateway. It fails by design. Use
> `verify-ca` with the instance CA instead.

## When the database doesn't answer

If the gateway cannot reach the real database within its login time limit, the agent sees:

```text theme={null}
FATAL:  the database server did not answer in time
```

Check that the gateway can reach the database host and port, and that the destination is allowed. A private or internal address needs a narrow entry in `GATEWAY_ALLOW_PRIVATE_DESTINATIONS` on a self-hosted instance. See [Hardening](/docs/self-hosting/hardening.md).

## Next steps

* [Review the trust and threat model](/docs/guides/trust-model.md).
* [Lock down Docker and Linux agents](/docs/guides/agent-lockdown.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.