Cloudflare Workers + Digital Ocean Managed Postgres DB
Hello, im trying to setup a simple JS POC using Cloudflare workers. tl;dr the worker will query a Postgres DB on Digital Ocean. And i can not make it work at all.
I have tried both
pg
pg
and
postgres
postgres
javascript libraries
node_compat
node_compat
is set to
true
true
import { Client } from "pg";const client = new Client({ connectionString: "postgresql://u:p@host:p/name", ssl: { cert: `a redacted cert`, },});export default { async fetch( request, env, ctx ) { await client.connect(); const resp = Response.json(/**some response */); // Close the database connection, but don't block returning the response ctx.waitUntil(client.end()); return resp; },};
import { Client } from "pg";const client = new Client({ connectionString: "postgresql://u:p@host:p/name", ssl: { cert: `a redacted cert`, },});export default { async fetch( request, env, ctx ) { await client.connect(); const resp = Response.json(/**some response */); // Close the database connection, but don't block returning the response ctx.waitUntil(client.end()); return resp; },};
Just running
client.connect
client.connect
here will make the worker freak out running when making a request to the endpoint. ( Starting the worker with
wrangler deploy src/index.js
wrangler deploy src/index.js
)
Errors out with
ErrorConnection terminated unexpectedly
ErrorConnection terminated unexpectedly
I have tested everything . Not sure what im doing wrong.