Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Neon 4 min read How-to mediumAdept

How to use Postgres at the Edge

Standard Postgres can't be reached from V8-isolate edge runtimes like Vercel Edge Functions because they don't support raw TCP. Neon's serverless driver, a drop-in replacement for node-postgres, gets around this by talking HTTP or WebSockets to a Neon proxy, which opens the TCP connection to Postgres. The walkthrough shows Next.js edge API routes using the `neon` SQL-over-HTTP function for single-shot queries and `Pool`/`Client` over WebSockets for multiple queries per connection. It then combines `@vercel/edge` geolocation with an INSERT into a locations table, as in the Ping Thing demo.

Related posts

  • Sub-10ms Postgres queries for Vercel Edge Functions

    Neon mediumAdept

    Neon's serverless Postgres driver now supports SQL-over-HTTP via fetch, and Neon's proxy adds opt-in experimental connection caching (`fetchConnectionCache`). Together they cut query latency from Vercel Serverless and Edge Functions by about 40%, bringing same-region one-shot queries to single-digit milliseconds. Use the `neon` HTTP function for a single query per invocation and `Pool` when a function runs multiple queries. Drizzle-ORM also supports the driver for type-safe queries.

  • Edge-compatible Serverless Driver for Postgres

    Neon mediumAdept

    Neon's @neondatabase/serverless driver lets V8-isolate platforms like Cloudflare Workers, which lack TCP, speak the real Postgres wire protocol by tunnelling it over a WebSocket to an open-source Go proxy that relays to TCP. The driver is node-postgres with shims for Node APIs, and it does end-to-end TLS 1.3 via WolfSSL compiled to WebAssembly with Asyncify, offloading crypto to SubtleCrypto. Because SCRAM-SHA-256's 4,096 hash rounds exceed Workers' 10–50ms CPU limits, it falls back to cleartext auth over mandatory TLS. The post also covers latency tradeoffs from the extra proxy hop, round-trip reductions, and how to self-host the proxy for any Postgres.

  • Neon Functions: backend logic next to your data

    Neon mediumAdept

    Neon Functions are long-running Node.js 24 compute deployed onto a Neon branch, in the same region and on the same infrastructure as its Lakebase Postgres database, with DATABASE_URL and other Neon backend credentials injected automatically. Running next to the data removes the cross-network hop per query and lets a function keep a pg Pool open across requests, so agents can stream for minutes and WebSockets or SSE can fan out via Postgres LISTEN/NOTIFY. Functions branch with the database: each child branch gets its own function copy and URL, and deleting the branch deletes it. They are declared in neon.ts, Hono is the recommended framework, and the pg Pool is recommended over the serverless driver; functions are not for frontend hosting or background jobs, and cron and event triggers are planned.

  • Web-based AI SQL Playground and connecting to Postgres from the browser

    Neon mediumAdept

    Neon's Postgres AI Playground connects to a Postgres database directly from the browser and uses OpenAI's gpt-3.5-turbo to turn natural-language prompts into SQL. Browsers can't open TCP connections, so the Neon serverless driver tunnels the Postgres wire protocol through a WebSocket-to-TCP proxy. The proxy is open source and can be self-hosted to reach other Postgres providers. A Vercel edge function rate-limits requests with Upstash Redis, injects the user's schema (pulled from information_schema.columns and flattened to a compact text form) into the prompt, and streams the model's response back to the editor.

  • @neondatabase/serverless hits 1.0.0

    Neon mediumAdept

    Neon's JavaScript/TypeScript serverless driver, which carries Postgres queries over HTTP and WebSockets for environments without raw TCP such as V8 isolates and browsers, reaches 1.0.0 after a refactor that adds auto-generated types via api-extractor and CI tests across Node, Bun, Deno, browsers, Cloudflare Workers, Vercel and the Prisma/Drizzle adapters. Tagged-template queries are now composable, because placeholder numbering is deferred until execution. A breaking change makes calling sql(...) as a plain function an error, since that form used ordinary string interpolation and invited SQL injection. New sql.query and sql.unsafe helpers cover the parameterized and trusted-raw-SQL cases. Binary inserts over HTTP now hex-encode roughly 10x faster, though WebSockets remain the recommended path for large blobs.

The quest for software engineering knowledge.

Look:
Layout: