postgres in pages

how can i use postgres using sveltekit with pages and drizzle ORM? im using pg atm but im getting these errors when building;
18:24:51.574 node_modules/pg/lib/stream.js:21:20:
18:24:51.574 21 │ var tls = require('tls')
18:24:51.574 ╵ ~~~~~
18:24:51.574
18:24:51.575 Cannot use "tls" when deploying to Cloudflare.
18:24:51.575
18:24:51.575
18:24:51.575 ✘ [ERROR] Could not resolve "fs"
18:24:51.575
18:24:51.575 node_modules/pgpass/lib/index.js:4:17:
18:24:51.575 4 │ , fs = require('fs')
18:24:51.575 ╵ ~~~~
18:24:51.576
18:24:51.576 Cannot use "fs" when deploying to Cloudflare.
18:24:51.574 node_modules/pg/lib/stream.js:21:20:
18:24:51.574 21 │ var tls = require('tls')
18:24:51.574 ╵ ~~~~~
18:24:51.574
18:24:51.575 Cannot use "tls" when deploying to Cloudflare.
18:24:51.575
18:24:51.575
18:24:51.575 ✘ [ERROR] Could not resolve "fs"
18:24:51.575
18:24:51.575 node_modules/pgpass/lib/index.js:4:17:
18:24:51.575 4 │ , fs = require('fs')
18:24:51.575 ╵ ~~~~
18:24:51.576
18:24:51.576 Cannot use "fs" when deploying to Cloudflare.
5 Replies
Giraffe
Giraffe7d ago
Did you find a solution to this?
Ahz
Ahz6d ago
you'll need to connect at the request level, not app level, serverless pages can't maintain a db connect also those libs dont work w cf workers, no fs access, there are other ways to connect to postgres, like neon
Maraciuca
Maraciuca5d ago
Had the same issues yesterday, this seems it would work (at least on a theoretical level, haven't tried it out yet)
Borut Svara
Borut Svara5d ago
Have you tried a serverless client https://orm.drizzle.team/docs/connect-vercel-postgres ? NOT TESTED
Drizzle ORM - Vercel Postgres
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
danim47c
danim47c5d ago
My recomendation is to use Cloudflare Hyperdrive. It manages a kind of connection pool between workers and your database. Also, postgres-js driver is more recommended for serverless environments. Note that you need to set https://developers.cloudflare.com/workers/configuration/compatibility-flags/#nodejs-compatibility-flag.

Did you find this page helpful?