Guide

Drizzle with Supabase from Cloudflare Workers: five things that break

Drizzle and Supabase are not two choices of the same thing. Supabase is a hosted Postgres platform; Drizzle is a TypeScript query layer that connects to any Postgres, including Supabase's. You can use both, and Supabase publishes its own Drizzle quickstart.

What the quickstart does not tell you is where it breaks when the client is a Cloudflare Worker rather than a laptop. These five are the ones we hit, in the order we hit them.

The five, at a glance

1. The connection string

The session pooler answers TCP and then never answers the TLS request.

2. TLS inside workerd

The handshake dies inside the Workers runtime even when it works from your laptop.

3. A hash in the password

The URL is cut at the character, and the credential silently becomes a different one.

4. Prepared statements

Transaction pooling gives your statement a different backend on the next round trip.

5. Migrations

A schema migration through a transaction pooler has no session to hold a lock or a transaction.

1. The connection string is not the one from the dashboard

A hosted Postgres project gives you more than one connection endpoint, and they are not interchangeable: one is a direct connection, one pools at the session and one pools at the transaction. Which one you need depends entirely on whether your runtime can hold a connection open, and a Worker cannot.

What we measured. Pointing the app at the session pooler on:5432, the TCP connection was accepted and the SSLRequest that followed was never answered — the connection hung until it was reset. The transaction pooler on :6543 is the endpoint that worked. Two endpoints, one project, one credential, and only one of them usable from this runtime.

# what failed from the Worker
postgresql://USER:PASSWORD@HOST:5432/postgres

# what worked
postgresql://USER:PASSWORD@HOST:6543/postgres

The general lesson is not the port number — it is that "the connection string" is a set, and a runtime that cannot hold a connection needs the pooled one. Check which endpoint you were given before you debug the driver.

2. TLS that works from your laptop and not from workerd

The same connection string that failed in step one also failed TLS independently: inside the Workers runtime the TLS handshake to that pooler died, while the identical connection over plaintext completed. On a laptop, the TLS connection worked. That difference is what makes this trap expensive — the credential is fine, the certificate is fine, and the test you ran locally passes.

What we did. Used the pooled endpoint that negotiates successfully rather than disabling TLS to force the failing one. Recorded it, because the next person to redeploy will do exactly the same thing and lose the same afternoon.

Treat a hanging SSLRequest as a routing question first and a certificate question second. In our case the endpoint was the variable, and the driver was never at fault.

3. A hash in the password truncates the URL

Connection URLs are parsed by rules older than your password policy. A # in the password starts a fragment, so everything after it is dropped: the connection is attempted with a credential that is a prefix of the real one, and the error you get names authentication, not parsing.

# wrong: the password becomes "abc", and everything after # is a fragment
postgresql://user:abc#def@host:6543/postgres

# right: percent-encode the character
postgresql://user:abc%23def@host:6543/postgres

The same class of bug covers @, : and /. If a generated password can contain them, encode the password before it reaches the URL rather than trusting that it will not.

4. Prepared statements under transaction pooling

This one is reasoning rather than a measurement, and it is worth stating as such. A prepared statement is created on a specific backend connection and referenced later by name. A transaction pooler hands your next transaction to whichever backend is free — which is the whole point of pooling, and it is incompatible with a statement that expects the session it was prepared in.

In practice this shows up as an intermittent failure under concurrency and never on a quiet development machine. With Drizzle on Postgres, the driver option is explicit:

import postgres from 'postgres';
import { drizzle } from 'drizzle-orm/postgres-js';

const sql = postgres(process.env.DATABASE_URL, {
  prepare: false,   // required behind a transaction pooler
});

export const db = drizzle(sql);

If you control the runtime and can hold a direct connection, prepared statements are fine and faster. The pooled configuration is a trade, not an upgrade — it exists so that a short-lived runtime can use the database at all.

5. Do not run migrations through the pooler

Schema migrations want a session: a transaction they hold open, advisory locks, and a connection that will still be the same one after the lock is taken. A transaction pooler gives you none of those guarantees, and a migration that half-applies behind a pooler is a much worse afternoon than one that refuses to start.

Run drizzle-kit against the direct connection (or a session-pooled one) from CI or from your machine, and let the application connect through the transaction pooler at runtime. Two connection strings in the configuration, for two different jobs.

# migrations: direct / session connection, run by CI or by hand
DIRECT_DATABASE_URL=... npx drizzle-kit migrate

# runtime: transaction pooler, inside the Worker
DATABASE_URL=...   # the :6543 endpoint

And RLS is still your problem

Drizzle is a query builder over a connection. It does not add row-level security, and it does not set the session variable your policies read. If your policies key oncurrent_setting('app.current_org'), the ORM is exactly the layer that has to set it — per transaction — no matter which of the endpoints above you chose.

The per-request setting pattern and the five ways a policy can still leak are inthe RLS bypass guide. The schema the policies sit on is inmulti-tenant Postgres design with Drizzle. If the platform choice is still open rather than made,Supabase vs Drizzleexplains why that is not a choice at all.

Get in touch

Questions about the product, team licenses, or anything else? We'll aim to respond within 48 hours.

Max 2000 characters

Stored in our own database — no third party. Deleted on request.

Related reading