Quotura

How to query Postgres from edge functions

Edge runtimes such as Cloudflare Workers and Vercel Edge cannot open ordinary Postgres TCP connections. Send SQL over HTTPS instead: create a Quotura API key, generate a Facet client, and pass it the http() adapter. Queries, interactive transactions and migrations then work from any runtime that has fetch.

Last reviewed

Steps

  1. Create a database in the dashboard and an API key for its project.
  2. Store the key as a secret in your edge platform, for example FACET_API_KEY.
  3. Generate the client with facet generate; Postgres clients import only the edge-safe entry point.
  4. Create the client with the http() adapter and query as usual.
import { FacetClient, http } from './generated/index.js';

export default {
  async fetch(req, env) {
    const db = new FacetClient({ adapter: http({ url: env.FACET_URL, apiKey: env.FACET_API_KEY, database: 'main' }) });
    return Response.json(await db.post.findMany({ take: 10 }));
  },
};

Can I use transactions over HTTPS?

Yes. db.$transaction(async (tx) => { ... }) opens a transaction on the server that holds a connection for you, and each statement inside it is a request. Transactions roll back automatically if they go idle.

Without the ORM

POST { "sql": "...", "params": [...] } to /v1/databases/<name>/query with Authorization: Bearer <key>. The response is { rows, rowCount, fields }.

Frequently asked questions

Is HTTPS slower than a TCP connection?

Each request has a little overhead, but edge functions avoid the connection setup a TCP client would need on every cold start. Keep the database near your users or servers to keep round trips short.