Neon's serverless driver
new, verified against @neondatabase/serverless 1.2.0
Code written for Neon often uses
@neondatabase/serverless
instead of a TCP driver, because edge runtimes and Workers have no raw TCP.
Hobbyist’s Postgres proxy speaks both of that driver’s protocols, on the same
port as Postgres itself (5432 by default):
| Driver API | What it sends | Served as |
|---|---|---|
neon(), sql.transaction(), Pool.query with poolQueryViaFetch |
POST /sql with JSON |
One fresh Postgres connection per request, through the proxy |
Pool, Client |
The Postgres protocol inside a WebSocket | A Postgres session through the proxy, for as long as the socket is open |
Both are ordinary connections as far as the rest of Hobbyist is concerned. A
sleeping database wakes on the first request, exactly as it does for
psql. Requests and open WebSockets count as activity, so a database an
app is querying does not sleep under it. The database name in the connection
string picks the project, as it does for every other client
(routing).
The driver’s defaults point at Neon’s hostnames, so two or three neonConfig
settings are always needed. Nothing else in your code changes.
On your own machine
Section titled “On your own machine”Plain http and ws on loopback. Use the connection string hobby connect
prints, and set these once, before the first query:
import { neon, neonConfig, Pool } from '@neondatabase/serverless'
// http://127.0.0.1:5432/sql instead of https://api.<your-host>/sqlneonConfig.fetchEndpoint = (host, port) => `http://${host}:${port || 5432}/sql`// ws://127.0.0.1:5432/v2neonConfig.wsProxy = (host, port) => `${host}:${port}/v2`neonConfig.useSecureWebSocket = false
const sql = neon(process.env.DATABASE_URL!)const rows = await sql`SELECT ${1}::int AS n`
const pool = new Pool({ connectionString: process.env.DATABASE_URL })const { rows: more } = await pool.query('SELECT now()')The || 5432 matters for Pool.query with poolQueryViaFetch: the driver
builds that request’s URL without the port, so fetchEndpoint is called with
an empty one. If your proxy is not on 5432, write your port there.
From an app or worker on the same box
Section titled “From an app or worker on the same box”Apps and workers reach their database at host.docker.internal:<proxyPort>,
the same address their DATABASE_URL already uses. Use the local settings
above, unchanged: traffic from the box’s own containers is allowed in
plaintext, as it is for TCP.
The default fetchEndpoint would send these requests to
https://api.docker.internal/sql, which does not exist, so code that
hardcodes the driver needs the neonConfig.fetchEndpoint line above before it
can work here.
From anywhere else, with TLS
Section titled “From anywhere else, with TLS”A request from another machine must arrive over TLS. Over TCP, SCRAM keeps
your password off the wire even without TLS; here it does not, because
/sql carries the password in a header, and the WebSocket login at the
driver’s default (pipelineConnect: "password") sends it in cleartext. So without a certificate, these endpoints answer only loopback and
the box’s own containers.
Give the proxy a certificate exactly as for TCP clients, with proxyTls and
proxyHost: "all"
(reaching a database from another machine).
The same port then serves https:// and wss:// with that certificate:
// https://db.example.com:5432/sql and wss://db.example.com:5432/v2neonConfig.fetchEndpoint = (host, port) => `https://${host}:${port || 5432}/sql`neonConfig.wsProxy = (host, port) => `${host}:${port}/v2`// useSecureWebSocket stays at its default, true
const sql = neon('postgresql://app:...@db.example.com:5432/blog')A plaintext request from another machine gets a 403 naming what to
configure. neon() shows the message. A Pool cannot: the WebSocket API
hides the response to a refused upgrade, so the driver reports only that the
connection failed. If a Pool fails from another machine and neon() works,
check useSecureWebSocket.
The certificate is the proxy’s own, not Caddy’s. Caddy is not involved, and
caddyEnabled does not matter here
(ADR 0019 explains why).
What is different from Neon
Section titled “What is different from Neon”- No connection pool behind
/sql. Each request opens a Postgres connection through the proxy and closes it after. That costs one backend start and one password exchange per request: a few milliseconds on loopback, not measured over a network. APoolover WebSockets keeps its sessions open and does not pay this. - The hostname is not the route. Neon picks an endpoint from the host in the connection string. Hobbyist picks a project from the database name, as for every other client.
- No JWT.
authTokenand anAuthorizationheader are refused. Put the password in the connection string. - One statement per query, as on Neon.
sqlruns each query as a prepared statement, and Postgres refuses several statements in one. neonConfig.pipelineConnectworks at its default,"password". The proxy answers Postgres’s SCRAM challenge on the client’s behalf, as Neon’s proxy does. WithpipelineConnect: falsethe driver does SCRAM itself, end to end.forceDisablePgSSLmust staytrue, its default. The WebSocket’s own TLS is the encryption. A PostgresSSLRequestinside it is answeredN.- Sizes. A
/sqlrequest or response over 10 MiB is refused, as Neon refuses one. One WebSocket message over 64 MiB closes the socket.
Issue #155 is where this came from.