# PostgreSQL and AWS RDS with a static IP

Source: https://prontaro.com/docs/integrations/postgres/

Keep port 5432 closed to the public internet while connecting to PostgreSQL and AWS RDS from serverless, CI, or behind a private NAT.

PostgreSQL uses its own binary wire protocol directly over TCP (port `5432`), so standard `HTTP_PROXY` variables do not apply. Outbound connections to an allowlisted cloud database run over `prontaro forward` or `socks5h://`, while inbound access to a private database uses a locked-down TCP tunnel with IP rules.

## Which scenario do you need?

| Scenario | Prontaro tool | Minimum plan |
| --- | --- | --- |
| **1. Connect from serverless, CI, or laptop to allowlisted RDS / Postgres** | `prontaro forward` or `socks5h://` | Dedicated ($12/mo) or Pro |
| **2. Give your team access to a private Postgres behind NAT without a VPN** | `prontaro tcp 5432` + `prontaro access` | Tunnels ($6/mo, persistent port on a shared IP) or Dedicated/Pro (port `5432` on your IP) |

---

## Scenario 1. Outbound connection to PostgreSQL / AWS RDS

Instead of opening your database to `0.0.0.0/0`, allowlist only your single dedicated Prontaro IPv4 (`/32`).

### Step 1. Allowlist your static IP on the database

#### AWS RDS

Open the Security Group attached to your RDS instance and add an inbound rule for TCP `5432` scoped to your `/32`:

**AWS Console → RDS / EC2 → Security Groups → Inbound rules**

- `Type / Port`: `PostgreSQL (TCP 5432)`
- `Source`: `203.0.113.10/32`
- `Description`: `prontaro-static-ip`

*(Your RDS instance must have `Publicly accessible = Yes` so external connections from your allowlisted IP can reach its endpoint.)*

#### Self-hosted PostgreSQL (`pg_hba.conf`)

Add a TLS-only (`hostssl`) rule in `pg_hba.conf` and reload PostgreSQL without a restart:

```text
# /etc/postgresql/16/main/pg_hba.conf
hostssl  app_db  app_user  203.0.113.10/32  scram-sha-256
```

```sql
SELECT pg_reload_conf();
```

### Step 2. Create credentials and verify the outbound IP

If you connect directly from serverless code over `socks5h://`, create dedicated proxy credentials in the dashboard (for `prontaro forward`, a logged-in `prontaro CLI` or `PRONTARO_KEY` is all you need). Verify the IP against `api.ipify.org` first, then restrict allowed destinations.

1. Open **IP addresses → Proxy credentials** in [my.prontaro.com](https://my.prontaro.com/ips), create credentials named `pg-prod`, and copy the `socks5h://...:1080` URL.
2. Leave destination rules open for the IP check below.

IP addresses Proxy credentials [Open in dashboard](https://my.prontaro.com/ips)

Confirm the SOCKS5 route exits from your dedicated static IPv4:

```
 curl -fsS --proxy "socks5h://u_pg:s3cr3t@<entry>.prontaro.io:1080"
203.0.113.10
```

Now lock `pg-prod` down to your PostgreSQL host on port `5432`:

1. Open **IP addresses → Proxy credentials** in [my.prontaro.com](https://my.prontaro.com/ips), select `pg-prod`, and switch to **Access**.
2. Add `*.rds.amazonaws.com:5432` (or your exact database host) under **To** and save.

IP addresses Proxy credentials Access [Open in dashboard](https://my.prontaro.com/ips)

### Step 3A. Connect via `prontaro forward` (`psql`, Prisma, SQLAlchemy, Go, CI)

Forward your remote PostgreSQL server to `127.0.0.1:15432`:

```bash
prontaro forward 15432 mydb.xxxx.eu-central-1.rds.amazonaws.com:5432 --ip 203.0.113.10
```

```text
prontaro forward 15432 mydb.xxxx.eu-central-1.rds.amazonaws.com:5432
  127.0.0.1:15432  →  mydb.xxxx.eu-central-1.rds.amazonaws.com:5432 from your IP 203.0.113.10
```

Verify the connection and check what IP PostgreSQL sees:

```
 psql "postgresql://app_user@127.0.0.1:15432/app_db?sslmode=verify-ca&sslrootcert=global-bundle.pem" -c "SELECT inet_client_addr();"
 inet_client_addr 
------------------
 203.0.113.10
(1 row)
```

> **Hostname verification (sslmode=verify-full) over 127.0.0.1**
>
> When connecting to `127.0.0.1:15432`, RDS presents a TLS certificate issued for `mydb.xxxx.eu-central-1.rds.amazonaws.com`, not `127.0.0.1`. As a result, `sslmode=verify-full` rejects the hostname mismatch.
>
> Two clean ways to handle this:
>
> 1. **Verify CA only:** use `sslmode=verify-ca` with the AWS RDS CA bundle (`global-bundle.pem`) — traffic stays encrypted and CA-verified, skipping only the `127.0.0.1` hostname check.
> 2. **Keep full verification:** keep `sslmode=verify-full` by mapping `127.0.0.1 mydb.xxxx.eu-central-1.rds.amazonaws.com` in `/etc/hosts` (connecting on port `15432`), or set TLS `servername` in your driver (`pg`, Go `pgx`).

### Step 3B. Connect via `socks5h://` from serverless (Node.js `pg` + `socks`)

In Vercel Functions, AWS Lambda, or Cloud Run — where running a background `prontaro forward` process is inconvenient — connect directly through `socks5h://`. Example with Node.js `pg` and `socks` (`npm install pg socks`), keeping strict TLS hostname verification (`servername`):

```js
// db.mjs
import { Client } from 'pg';
import { SocksClient } from 'socks';

const proxy = new URL(process.env.PRONTARO_SOCKS_URL); // socks5h://...
const dbHost = process.env.PGHOST;
const dbPort = Number(process.env.PGPORT ?? 5432);

const { socket } = await SocksClient.createConnection({
  proxy: {
    host: proxy.hostname,
    port: Number(proxy.port),
    type: 5,
    userId: decodeURIComponent(proxy.username),
    password: decodeURIComponent(proxy.password),
  },
  command: 'connect',
  // Pass the hostname so DNS resolves on the Prontaro node (socks5h)
  destination: { host: dbHost, port: dbPort },
  timeout: 10_000,
});

const client = new Client({
  host: dbHost,
  port: dbPort,
  database: process.env.PGDATABASE,
  user: process.env.PGUSER,
  password: process.env.PGPASSWORD,
  stream: () => socket,
  ssl: {
    rejectUnauthorized: true,
    servername: dbHost,
  },
});

await client.connect();
const { rows } = await client.query('SELECT inet_client_addr() AS ip');
console.log('Connected from static IP:', rows[0].ip);
await client.end();
```

---

## Scenario 2. Secure inbound access to a private database via `prontaro tcp 5432`

If PostgreSQL runs on an office server, private subnet, or workstation behind NAT, you can expose it safely to trusted clients without setting up a VPN or router port forwarding.

New TCP tunnels are closed to everyone by default — packets from non-allowed IPs are dropped by the kernel filter on the Prontaro node before a TCP handshake completes.

### Step 1. Start the TCP tunnel on the database host

On Dedicated and Pro, open standard port `5432` directly on your dedicated IPv4 (on Tunnels, pass `--to shared` to get a persistent high port on a shared IP):

```bash
prontaro tcp 5432 --name pg-private --to 203.0.113.10
```

After starting the tunnel, allow only the source IPv4 addresses or CIDR ranges that should reach the database.

### Step 2. Allow only trusted source IPs

#### In the dashboard

1. Open **Tunnels → Access** in [my.prontaro.com](https://my.prontaro.com/) and select `pg-private`.
2. Add the allowed IPv4 addresses or CIDR blocks and save.

Tunnels Access [Open in dashboard](https://my.prontaro.com/tunnels)

#### In the terminal (CLI)

Allow an explicit IPv4 address or CIDR range (for example, your office or CI server IP):

```bash
prontaro access pg-private allow 198.51.100.7
```

Expected output:

```text
✓ pg-private now open only to 198.51.100.7/32 (was: closed); takes effect within seconds.
```

### Step 3. Connect from an allowed machine

```
 psql "postgresql://app_user@203.0.113.10:5432/app_db?sslmode=require" -c "SELECT version();"
PostgreSQL 16.4 ...
```

> **Do not pass --proxy-protocol directly to PostgreSQL**
>
> PostgreSQL does not understand HAProxy PROXY protocol headers and will immediately close the connection. Enable `--proxy-protocol` only when a proxy (such as nginx `stream` or HAProxy) sits in front of PostgreSQL and is configured to parse it.

To keep the tunnel running across reboots, install it as a background service (`prontaro service install -- tcp 5432 --name pg-private --to <your-IP>`) — see [Run as a service](https://prontaro.com/docs/cli/service.md).

## Common errors

| Symptom | Cause | Fix |
| --- | --- | --- |
| `no pg_hba.conf entry for host "203.0.113.10"` | IP is missing from `pg_hba.conf` or config wasn’t reloaded. | Add `<your-IP>/32` to `pg_hba.conf` and run `SELECT pg_reload_conf();`. |
| Connection to AWS RDS times out | `<your-IP>/32` is missing from the Security Group, or RDS `Publicly accessible` is off. | Check Security Group inbound rules (TCP `5432`) and confirm RDS public accessibility. |
| `certificate is valid for ..., not 127.0.0.1` | `sslmode=verify-full` used over `prontaro forward` on `127.0.0.1`. | Use `sslmode=verify-ca` with `sslrootcert`, or map the hostname in `/etc/hosts` / driver `servername`. |
| `403` from SOCKS5 proxy | Database host is not in the **To** list, or `socks5://` was used without `h`. | Use `socks5h://` and allow `*.rds.amazonaws.com:5432` in the credentials’ **Access** tab. |
| Connection to `prontaro tcp 5432` times out | New TCP tunnels start closed to everyone. | Run `prontaro access pg-private allow 198.51.100.7` (or add the IP in the dashboard). |

## Next steps

- [Connect to MongoDB Atlas](https://prontaro.com/docs/integrations/mongodb-atlas.md) — connect to Atlas clusters over `socks5h://` or `prontaro forward`.
- [Local port forwarding (`prontaro forward`)](https://prontaro.com/docs/outbound/forward.md) — full reference for outbound TCP forwarding and TLS certificate verification.
- [TCP tunnels](https://prontaro.com/docs/tunnels/tcp.md) — expose databases and other TCP services on your dedicated IPv4 or a persistent port on a shared IP.
- [SSH, SFTP, and databases behind NAT](https://prontaro.com/docs/tunnels/ssh-and-db.md) — practical setups for secure access to private servers without a VPN.
- [Access control & firewalls](https://prontaro.com/docs/tunnels/access.md) — restrict inbound tunnel traffic to approved IPv4 addresses and CIDR blocks.
