REST APIs on the Edge: Postgres Meets Cloudflare Workers

Postgres remains a cornerstone of modern data infrastructure—reliable, feature-rich, and the engine behind popular developer platforms like Hasura and Supabase. Its flexibility makes it a go-to choice for everything from traditional web apps to real-time, interactive services. For many developers, REST APIs are the primary interface to that data, yet connecting an application to a Postgres instance often means repeating the same boilerplate connection logic project after project.

Cloudflare Workers, the serverless functions platform, offers a different path. A new tutorial from Cloudflare demonstrates how to connect Workers functions directly to Postgres using PostgREST, an open-source tool that generates a standards-compliant REST API from a Postgres schema. This setup allows you to run a fully functional API layer at the edge, with Workers handling requests, authentication, and caching in front of your database—no additional server infrastructure required.

Why PostgREST and Workers Fit Together

PostgREST is battle-tested, powering REST interfaces for startups like Retool and Supabase. It exposes your Postgres data through conventional REST endpoints with sensible defaults. On the Workers side, the postgrest-js JavaScript library provides a straightforward way to query that endpoint using familiar JS primitives, as shown in the example from the tutorial:

// Get all users with at least 100 followers
const { data: users, error } = await client
.from('users')
.select(‘*’)
.gte('followers', 100)

Pairing PostgREST with Workers unlocks several architectural advantages:

  • You can build and deploy a distributed API without altering your Postgres configuration.
  • Workers KV and Durable Objects can serve as a globally-distributed cache layer in front of the database.
  • WebSockets and other real-time features work alongside Postgres, so you don’t need to migrate to a different database platform.
  • Cloudflare Pages can host your frontend, creating a fully edge-based application stack.

Keeping Your Database Private with Cloudflare Tunnel

Exposing a database directly to the public internet is rarely a good idea. Cloudflare Tunnel solves this by creating a secure connection between your local PostgREST server and the Cloudflare network. The API endpoint becomes publicly accessible through a safe tunnel, while the underlying database itself remains shielded from direct external access. This approach lets you expose just the REST API—not the entire database.

A Different Approach to Scaling

Scaling Postgres often involves adding read replicas and duplicating data across instances. With Workers and PostgREST, you can reduce that pressure by placing intelligent caching logic directly in front of the database. Workers’ architecture supports high-performance functions that intercept queries and serve cached responses via KV or Durable Objects, cutting down the load on your primary Postgres instance while maintaining a responsive developer experience.

Getting Started

Cloudflare has published a step-by-step tutorial covering the full setup. Two open-source repositories accompany it:

  • cloudflare/postgres-postgrest-cloudflared-example — for establishing a secure, tunnel-backed Postgres + PostgREST endpoint.
  • postgrest-worker-example — a working example of using postgrest-js inside Cloudflare Workers.

The tutorial also links to a free video course on building serverless APIs with Workers, plus the Cloudflare Developers Discord community for further discussions.