pg example not working

I followed the example from the Cloudflare documentation for connecting to Postgres using node-postgres / pq at the link: https://developers.cloudflare.com/hyperdrive/configuration/connect-to-postgres/#node-postgres–pg. However, I encountered the error “this.stream.once is not a function.” According to this compatibility matrix (https://workers-nodejs-compat-matrix.pages.dev/), nodejs_compat_v2 should support the .once method on streams. Is there a related issue or a step I might have overlooked? My code is based on the documentation, and you can view it here: https://github.com/waigel/example-pg-cloudflare. Any suggestions or help would be appreciated!
Cloudflare Docs
Connect to PostgreSQL | Hyperdrive
Hyperdrive supports PostgreSQL and PostgreSQL-compatible databases, popular drivers and Object Relational Mapper (ORM) libraries that use those drivers.
3 Replies
Mike Nomitch
Mike Nomitch7mo ago
Hey @mike, I'll dig in here in a bit. If you set node_compat = true in your wrangler.toml instead of compatability_flags = ["nodejs_compat_v2"] do you know if it works? It should work with V2 and node_compat = true is meant to be replaced, but it'd be helpful for debugging to see if it works in this case. Update on this one: It works now with pg and nodejs_compat if you're on the latest wrangler and the latest version of pg! There was some feature detection in the package that we needed to push an upstream patch to fix and then a crypto-related bug in unenv we also patched that was surfaced too. Thanks for the feedback on this one!
deanmauro
deanmauro3d ago
I'm running into this same error. All is working fine locally but once deployed--this.stream.once is not a function. pg version 8.12.0 wrangler.json:
{
"compatibility_date": "2025-03-29",
"main": "./src/index.ts",
"keep_vars": true,
"upload_source_maps": true,
"compatibility_flags": [
"nodejs_compat",
"nodejs_als"
]
...
}
{
"compatibility_date": "2025-03-29",
"main": "./src/index.ts",
"keep_vars": true,
"upload_source_maps": true,
"compatibility_flags": [
"nodejs_compat",
"nodejs_als"
]
...
}
This started when I replaced node_compat with nodejs_compat. Has something changed since the patch in October that I have to update?
deanmauro
deanmauro3d ago
Ah nevermind. The Github Action I'm using to deploy had an old version of wrangler set. Once updated, the error is gone.
No description

Did you find this page helpful?