Running Postgres on Cloudflare Containers

Abhishek Chanda

  ·  2 min read

Disclaimer: I work for Cloudflare. The views and opinions expressed in this post are my own and do not necessarily reflect the official policy or position of Cloudflare.

Cloudflare recently launched Cloudflare Containers allowing running containerized applications with workers. To use this, an user would need to define an application in a dockerfile, define a worker, and bind the container to that worker. Naturally, I was curious if we can run Postgres on the new container platform.

I decided to use Postgrest in the front as a REST API and to use that API from my worker. My dockerfile installs and starts both Postgres and Postgrest, it looks like this

dockerfile

The container runs a shell script that installs Postgrest and waits for Postgres to start, it looks like this

startsh

Postgrest is configured to connect to postgres on localhost on port 5432, and listens on port 3000

postgrestconf

The final piece is the actual worker, which is a HonoJS app and simply calls out to postgrest on port 3000. This is an example request

request

To deploy this, clone the repo https://github.com/achanda/cloudflare-postgres-containers, cd to the target directory and use wrangler to deploy. Once the api is up access it using curl

curl

Note that it might take a while for the container to start, the api will return an error while that happens. Once the container is up then subsequent requests should be instantaneous.