1. Access
Go to your Cloudflare dashboard under "Access" and generate a new service token.
Copy and save the Client ID and Client Secret, you will need this later to connect to the database.
Create an access policy for your specified hostname using the service token.
2. Argo Tunnel
Install cloudflared on the server where your database is running. If you are using a managed database, you can install it on a nearby VM.
brew install cloudflare/cloudflare/cloudflaredStart the tunnel in db-connect mode, providing a hostname and your database connection URL.
cloudflared db-connect --hostname db.myzone.com --url postgres://user:pass@localhost?sslmode=disable --insecureIf you want to deploy using Docker or Kubernetes, see our guide here. You can alternatively specify the following environment variables: TUNNEL_HOSTNAME and TUNNEL_URL.
Import the db-connect library in your Cloudflare Workers or browser project.
npm i @cloudflare/db-connectNow initalize the client and start coding!
import { DbConnect } from '@cloudflare/db-connect'
const db = new DbConnect({
host: 'db.myzone.com',
clientId: 'xxx',
clientSecret: 'xxx'
})
async function doPing() {
const resp = await db.ping()
}

