# Connecting

> Credentials, hostnames, TLS and what the gateway supports.

Clients connect to Elefork's gateway. The gateway authenticates you, wakes the branch if needed, checks it is talking to the right instance, and then passes traffic through unchanged.

## Credentials

```sh
elefork branch credentials br_... [--role app] [--ttl 3600]
```

or `POST /v1/branches/{id}/credentials`. The response has `host`, `port`, `user`, `database`, a `password` and a ready-to-use `uri`.

- The **password is a JWT** for one branch and one database role. It is valid for `ttl_seconds` (60 s to 24 h, default 1 h).
- **Revoking** (`elefork branch credentials br_... --revoke`, or `POST /v1/branches/{id}/credentials/revoke`) invalidates every token issued so far. New connections are refused within about 10 seconds; sessions that are already open stay open.

## Hostname and TLS

The hostname selects the branch: `br-abc123.db.elefork.com` for branch `br_abc123`. Branch ids contain `_`, which is not valid in hostnames, so hostnames use `-` instead.

TLS is required. Use `sslmode=verify-full`. libpq verifies against `~/.postgresql/root.crt` by default; to use the system's trusted CAs instead (libpq 16+), set `PGSSLROOTCERT=system` or add `sslrootcert=system` to the connection string. If your client cannot send the hostname (SNI), connect to any address and pass the branch in `options`:

```
psql "host=db.elefork.com sslmode=require user=postgres options=endpoint=br_abc123"
```

PostgreSQL 17+ clients can use `sslnegotiation=direct` to save a round trip.

## What works

Everything a normal session can do: prepared statements, `COPY`, `LISTEN`/`NOTIFY`, temporary tables, `SET`, and query cancel. Each client connection is one server session (no transaction pooling). Replication connections are not supported.

## Instance identity

Before sending any credential to a database, the gateway checks that instance's certificate. It must be issued for exactly this branch and generation. A stale or wrong address can therefore never put your session on someone else's database.
