Deno logoDeno
Deno v1

Connecting to Postgres from the edge

Luca Casonato

Postgres is one of the most popular databases. It is fast, familiar, and featureful. It is the first choice of database for many companies. One drawback of Postgres: applications can only connect to it via TCP - a protocol that is not supported by many serverless at edge runtimes (e.g. Cloudflare Workers, Vercel Edge Functions, or Netlify Edge Handlers).

This is often a problem with serverless at edge products. They don't have the same capabilities as an application running inside of a VM, or a container on Kubernetes. This makes them a non-starter for many that need to integrate into an existing system.

With Deno Deploy, we are building a more serverless at edge system with more capabilities. Developers should be able to build locally as they normally do: connect to Postgres, read static files from disk, and use environment variables for configuration. Then be able to deploy globally to our 28 regions across the world without additional boilerplate, configuration, or concerns about missing capabilities.

So with that: how do we connect to Postgres from edge? Well, you import your driver, connect to the database as usual, and then run queries... There is really nothing special to it.

On Deno Deploy you can connect to your Postgres databases (even with TLS and custom CA certificates) from the edge. You can also connect to other databases with non-HTTP protocols like Redis, MySQL, or MongoDB. To take full advantage of the global nature of Deno Deploy, you could use the Postgres interface to connect to a globally distributed Cockroach DB database. Or to a global Google Spanner instance using its new Postgres interface.

Heck, you could even connect to more obscure systems like MQTT, or even manage your Minecraft game server using Minecraft RCON.

If you want a more detailed rundown of using Postgres on Deno Deploy, check out our Postgres tutorial in the Deno Deploy documentation. You can also check out the Deno documentation on Deno.connect, Deno.connectTls, and Deno.startTls - the APIs used create outbound TCP and TLS connections from Deno and Deno Deploy.