# Connect an app to a managed database

## TLS in plain words

Traffic between your app and a Zaroz database is always encrypted. Your app also checks that the server is really your database, so nobody in between can read the data or pretend to be the database. That is TLS. We require it on every connection.

What you need to do depends on where your app runs:

- **On Zaroz** (an app or a worker in any of your projects): nothing. Every Zaroz container already trusts the Zaroz database certificate.
- **Anywhere else** (your laptop, another cloud): download the certificate once, then point your client at it with the setting shown below for your language.

## From a Zaroz app: connect in one step

Open the database, choose **Connect to an app**, and pick the app. Zaroz adds these variables to the app and restarts it:

| Variable | Value |
|---|---|
| `DATABASE_URL` | `postgresql://USER:PASSWORD@db-NAME:5432/DATABASE?sslmode=verify-full` |
| `PGHOST`, `PGPORT`, `PGDATABASE`, `PGUSER`, `PGPASSWORD` | the same parts, for clients that read them |

They are **linked**, not copied. When you rotate the password or the address changes, Zaroz updates the app on its next restart. In the app's Environment tab they show as *Linked · NAME*. Choose *Unlink* to turn one into a plain value you edit yourself.

You can also link a variable from the app's side. In **Add variable**, type a name such as `DATABASE_URL` and pick the suggestion, or open **Insert from your project**.

Every Zaroz container has the certificate at `/etc/zaroz/ca/database-ca.pem`. Zaroz also sets:

| Variable | Points at | Used by |
|---|---|---|
| `NODE_EXTRA_CA_CERTS` | `/etc/zaroz/ca/database-ca.pem` | Node.js (node-postgres, Prisma's Node driver adapters) |
| `PGSSLROOTCERT` | `/etc/zaroz/ca/ca-bundle.pem` | libpq: psycopg, PHP PDO, pgx, `psql` |
| `ZAROZ_CA_DIR` | `/etc/zaroz/ca` | your own scripts |

`ca-bundle.pem` holds the usual public certificate authorities plus Zaroz's, so a connection to a database outside Zaroz keeps working too. If you set one of these variables yourself, your value wins.

### Snippets that work inside Zaroz

```js
// node-postgres
import pg from "pg";
const pool = new pg.Pool({ connectionString: process.env.DATABASE_URL });
```

```prisma
// Prisma: schema.prisma, DATABASE_URL as Zaroz sets it
datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}
```

```python
# psycopg 3
import os, psycopg
conn = psycopg.connect(os.environ["DATABASE_URL"])

# SQLAlchemy
from sqlalchemy import create_engine
engine = create_engine(os.environ["DATABASE_URL"].replace("postgresql://", "postgresql+psycopg://", 1))
```

```go
// pgx
pool, err := pgxpool.New(ctx, os.Getenv("DATABASE_URL"))
```

```java
// JDBC (pgjdbc does not read PGSSLROOTCERT, so name the file)
String url = "jdbc:postgresql://" + System.getenv("PGHOST") + ":" + System.getenv("PGPORT") + "/"
    + System.getenv("PGDATABASE") + "?sslmode=verify-full&sslrootcert=/etc/zaroz/ca/database-ca.pem";
Connection c = DriverManager.getConnection(url, System.getenv("PGUSER"), System.getenv("PGPASSWORD"));
```

```php
// PHP PDO
$pdo = new PDO(
  sprintf("pgsql:host=%s;port=%s;dbname=%s;sslmode=verify-full", getenv("PGHOST"), getenv("PGPORT"), getenv("PGDATABASE")),
  getenv("PGUSER"), getenv("PGPASSWORD"),
);
```

## From outside Zaroz

1. Turn on **Public access** for the database. Its address is `NAME.db.zone.ZONE.zaroz.cloud`.
2. Download the certificate from the database's **Connect** tab (**Download certificate**). Save it next to your app, for example as `zaroz-ca.pem`.
3. Use the setting for your client, with the **full path** to the file:

| Client | Setting |
|---|---|
| node-postgres | `?sslmode=verify-full&sslrootcert=/path/to/zaroz-ca.pem`, or `NODE_EXTRA_CA_CERTS=/path/to/zaroz-ca.pem` |
| Prisma | `?sslmode=require&sslcert=/path/to/zaroz-ca.pem&sslaccept=strict` |
| psycopg, SQLAlchemy, `psql`, PHP PDO | `sslmode=verify-full sslrootcert=/path/to/zaroz-ca.pem` (or `PGSSLROOTCERT`) |
| pgx | `?sslmode=verify-full&sslrootcert=/path/to/zaroz-ca.pem` |
| JDBC | `?sslmode=verify-full&sslrootcert=/path/to/zaroz-ca.pem` |

The certificate is the same for every Zaroz database and does not change when you restore a backup.

Complete, ready-to-run code for every client, with each library's exact TLS settings: [Connect to PostgreSQL](/databases/postgres/connect).

## When it does not connect

| The error says | What it means | Fix |
|---|---|---|
| `ENOENT ... zaroz-ca.pem` | The URL names a certificate file your app does not have | Inside Zaroz, remove `sslrootcert` from the URL. Outside, use the full path to the downloaded file |
| `SELF_SIGNED_CERT_IN_CHAIN`, `certificate verify failed`, `unable to get local issuer certificate` | Your client does not trust the Zaroz certificate | Inside Zaroz, restart the app so it picks up `/etc/zaroz/ca`. Outside, follow the table above |
| `hostname mismatch`, `does not match host name` | You connect to an address the certificate does not cover | Use the address the Connect tab shows |
| `no pg_hba.conf entry ... no encryption` | Your client tried without TLS | Add `sslmode=verify-full` (`require` for Prisma) |

Never use `sslmode=no-verify` or `rejectUnauthorized: false` as a fix. It turns off the server check, which is the part that protects you.
