October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk5 min

Deploying SvelteKit to Cloudflare Pages with a Real Database: D1, Postgres, and the Gotchas Nobody Mentions

How to deploy a dynamic SvelteKit app to Cloudflare Pages with D1 or PostgreSQL through Hyperdrive, including build settings, bindings, Node.js compatibility and the mistakes that trip people up.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A dynamic SvelteKit app on Cloudflare uses @sveltejs/adapter-cloudflare. For data, you have two documented routes. D1 is Cloudflare’s own database, reached through a binding. PostgreSQL is reached through Hyperdrive, and its drivers need Node.js compatibility turned on. Most deployment problems come from mismatched build directories, binding names, and runtime flags, not from SvelteKit itself.

One decision comes before any of that. Cloudflare now recommends Workers for new projects, so check whether Pages is the right target before you commit to it.

As an Amazon Associate I earn from qualifying purchases.

Pages or Workers: decide first

Cloudflare’s Pages framework guides index (last updated 2026-08-21) says Workers supports most Pages use cases, has a broader feature set, and is Cloudflare’s primary platform for applications. It recommends Workers for new projects. Pages is not described as discontinued, and the SvelteKit-on-Pages guide is still current. The rest of this article follows that Pages route.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • Existing Pages project: the steps below apply directly.
  • Starting fresh: read Cloudflare’s Workers guidance for SvelteKit before you pick Pages. The adapter, platform.env bindings, D1 and Hyperdrive concepts carry over. The deployment configuration is the part that differs.

D1 or Postgres: what actually decides it

Cloudflare’s documentation supports a distinction in kind, not a ranking. It does not give a latency, price, SQL feature-parity or migration comparison. Don’t pick on claims about speed or cost that you can’t trace to a source.

Question D1 PostgreSQL via Hyperdrive
What is it? Cloudflare’s native serverless database, accessed through a binding An existing PostgreSQL database, reached through a Hyperdrive binding
Choose it when… You’re building new and the native binding model suits the app You need PostgreSQL compatibility or already have a Postgres database
Runtime requirement Binding declared and exposed on platform.env Node.js compatibility (nodejs_compat) for drivers such as Postgres.js
Code access platform.env.DB (name is yours to choose) platform.env.HYPERDRIVE (name is yours to choose)

Sources: Cloudflare’s D1 and SvelteKit example, Pages bindings and Hyperdrive PostgreSQL guide.

Set up the project and adapter

Cloudflare’s SvelteKit guide for Pages offers two starting points.

New project with create-cloudflare

npm create cloudflare@latest -- my-svelte-app --framework=svelte --platform=pages

The C3 tool installs Wrangler and the adapter for you.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Existing SvelteKit project

Install @sveltejs/adapter-cloudflare and set it as the adapter in svelte.config.js. Commit the change before you deploy. Pages builds from your repository, so an uncommitted adapter switch won’t reach the build.

Get the build settings right

For dashboard or Git-based deployments, Cloudflare’s build configuration page lists the SvelteKit preset as:

  • Build command: npm run build
  • Build output directory: .svelte-kit/cloudflare

Pages can rebuild on each pushed commit and create preview deployments for pull requests.

The output directory depends on the adapter. If the project uses adapter-static, which produces client-side assets with no server-side rendering, the output directory is build, and you must set that in Pages. A database-backed app needs server code, so it should be on the Cloudflare adapter. If a deploy “succeeds” but serves nothing useful, check this setting first, and don’t copy a directory from another framework’s tutorial.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Connecting D1

Cloudflare’s D1 guide uses a SvelteKit server endpoint with D1 bound to the Pages Function. Inside a SvelteKit handler, the binding is on the platform argument, typically platform.env.DB.

Declare the binding type

For TypeScript, declare the binding as D1Database on App.Platform in src/app.d.ts:

declare global {
  namespace App {
    interface Platform {
      env: {
        DB: D1Database;
      };
    }
  }
}
export {};

Query it from an endpoint

Cloudflare’s example uses a prepared statement and returns JSON. The shape is as follows (the table and column names are placeholders, not from Cloudflare’s example):

// src/routes/api/posts/+server.ts
import { json } from '@sveltejs/kit';

export async function GET({ platform }) {
  const { results } = await platform.env.DB
    .prepare('SELECT id, title FROM posts WHERE published = ?')
    .bind(1)
    .all();
  return json(results);
}

Run it locally

Pass the binding to Wrangler with --d1 BINDING_NAME=DATABASE_ID. Cloudflare’s Pages bindings page gives the form as wrangler pages dev <OUTPUT_DIR> --d1 BINDING_NAME=DATABASE_ID, while the D1 SvelteKit example shows it with wrangler dev. Use the command from the page that matches your project’s setup. The binding name must match your code. Wrangler persists local data to local storage by default. That means rows you create while developing are not in your production database, and the reverse also holds.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Connecting PostgreSQL through Hyperdrive

Cloudflare documents Hyperdrive as the way for Workers and Pages Functions to reach an existing database, PostgreSQL included. Pages supports a Hyperdrive binding in its Wrangler configuration. Cloudflare’s PostgreSQL example uses a binding named HYPERDRIVE and the Postgres.js client.

The Node.js compatibility requirement

This is the gotcha that costs the most time. Cloudflare states that PostgreSQL drivers such as Postgres.js depend on Node.js APIs. Pages Functions that use Hyperdrive must therefore be deployed with Node.js compatibility. The documented configuration has the nodejs_compat compatibility flag and a compatibility date. Adding a Hyperdrive binding alone doesn’t make a Node-oriented driver work.

A Wrangler configuration has this shape. The IDs are placeholders, and you should copy the exact fields from Cloudflare’s current pages for your Wrangler version:

compatibility_date = "YYYY-MM-DD"
compatibility_flags = ["nodejs_compat"]

[[hyperdrive]]
binding = "HYPERDRIVE"
id = "<YOUR_HYPERDRIVE_CONFIG_ID>"

Using it in a handler

import postgres from 'postgres';

export async function GET({ platform }) {
  const sql = postgres(platform.env.HYPERDRIVE.connectionString);
  const rows = await sql`SELECT id, title FROM posts LIMIT 10`;
  return new Response(JSON.stringify(rows), {
    headers: { 'content-type': 'application/json' }
  });
}

Declare HYPERDRIVE in App.Platform in the same way as DB above. Then test on the actual deployed runtime, not only locally. The flags and compatibility date that make a driver load are exactly the things that can differ between your dev server and a Pages deployment.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The documentation supports connecting to an existing PostgreSQL database in general. It doesn’t vouch for any particular managed-Postgres provider, so check your provider’s own notes on connecting from Cloudflare.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Gotchas that apply to either database

Root /functions files are ignored

SvelteKit on Pages compiles to a single _worker.js. Handlers in a root /functions directory, the usual Pages Functions convention, are not included. Write your logic as SvelteKit endpoints (+server.ts) or in +page.server.ts load functions and actions.

Binding names must agree everywhere

The name in your Wrangler configuration, the --d1 flag and your code (platform.env.DB) must be identical, as must the TypeScript declaration. A mismatch usually shows up as platform.env.X being undefined at request time.

Dashboard binding changes need a redeploy

If you add or change a binding in the Pages dashboard instead of the configuration file, it only takes effect after you redeploy, per Cloudflare’s bindings documentation. Adding a binding and refreshing the live site won’t show it.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Local data is not production data

A table that works on your machine and is missing in production usually means the schema was applied only to the local database. Apply schema changes to the remote database as a separate step.

Where this leaves you

Use the Cloudflare adapter and .svelte-kit/cloudflare for a database-backed Pages deployment. Choose D1 if you want the native binding model, or Postgres through Hyperdrive if you need PostgreSQL or already run it, and then enable nodejs_compat. For a brand-new project, compare it against Workers first, since that is where Cloudflare is steering new applications.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.