
pg driver, but if we're using @neondatabase/serverless or similar I'd expect their optimizations to reduce round trips would make it closer at leastConnection terminated unexpectedly error thrown while trying to query my Postgres DB w/ Hyperdrive. Using almost exactly the example code here:client.connect() is finishing fine, but client.query() is throwing Connection terminated unexpectedly . I'm setting node_compat = true in my wrangler.toml as well. Any ideas?rds-ca-rsa2048-g1, the issue fixed itself when changing back to the default rds-ca-2019 . Definitely a confusing error message (not sure if this is a pg issue, or a Workers issue via the connect call), but hopefully this can help someone else!connect() directly?pg (not sure what Hyperdrive->AWS is using) doesn't verify certificates
connect() directly w/ sslmode=REQUIRE doesn't verify the certs, so it works as expectedrequire (and prefer, but we ultimately require TLS).const client = new Client({
connectionString: context.env.DB_HYPERDRIVE.connectionString,
});
// Connect to our database
console.log("START CONNECT");
await client.connect();
console.log("END CONNECT");
// Test query
console.log("START QUERY");
const result = await client.query({
text: "SELECT * FROM pg_tables",
});
console.log("END QUERY");
// Returns result rows as JSON
return context.json({ result: result });import { Client } from 'pg';
import { Hono, MiddlewareHandler } from 'hono';
type Bindings = {
HYPERDRIVE: Hyperdrive;
};
const app = new Hono<{
Bindings: Bindings;
Variables: {
DB: Client;
};
}>();
const dbMiddleware: MiddlewareHandler = async (c, next) => {
c.set('DB', new Client({ connectionString: c.env.HYPERDRIVE.connectionString }));
await next();
};
app.get('/', dbMiddleware, async (c) => {
try {
// Connect to our database
await c.var.DB.connect();
// Test query
let result = await c.var.DB.query({ text: 'SELECT * FROM pg_tables' });
// Return result rows as JSON
return c.json(result);
} catch (e) {
console.log(e);
return Response.json({ error: JSON.stringify(e) }, { status: 500 });
}
});
export default app;