Skip to content
Discord Get Started

SvelteKit

SvelteKit connects to DB9 through any standard PostgreSQL driver over pgwire. DB9 is PostgreSQL-compatible, so no special adapter is needed.

This guide shows how to wire up a SvelteKit app with DB9 using Prisma for schema-first workflows and Drizzle for TypeScript-first query building.

  • A DB9 database (create one)
  • Node.js 18+
  • SvelteKit 2+
Terminal
db9 create --name sveltekit-app

db9 create prints the connection string, with the admin password, once — copy it from the Connection String: line. It has this shape:

Output
postgresql://YOUR_TENANT_ID.admin:YOUR_PASSWORD@pg.d0000.db9.io:5432/postgres

YOUR_TENANT_ID is the tenant ID prefix in the generated connection string. Copy the username from the Connection String: line printed by db9 create or from db9 db connect. The examples below use pg.db9.io:5433, which reaches the same server; either host and port works. db9 db status sveltekit-app shows the connection string without the password; if you lose it, db9 db reset-password sveltekit-app sets a new password and prints a new connection string.

Set the connection string as an environment variable:

.env
DATABASE_URL="postgresql://YOUR_TENANT_ID.admin:YOUR_PASSWORD@pg.db9.io:5433/postgres?sslmode=require"
Terminal
npx sv create sveltekit-db9
cd sveltekit-db9
npm install prisma@6 @prisma/client@6
npx prisma init
prisma/schema.prisma
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model Todo {
id Int @id @default(autoincrement())
title String
done Boolean @default(false)
createdAt DateTime @default(now())
}

Push the schema to DB9:

Terminal
npx prisma db push

This first push works against an empty database. Later schema changes need raw SQL — see Troubleshooting.

Generate the client:

Terminal
npx prisma generate

Create a shared Prisma instance to avoid connection exhaustion during development:

src/lib/server/prisma.ts
import { PrismaClient } from '@prisma/client';
const globalForPrisma = globalThis as unknown as { prisma: PrismaClient };
export const prisma = globalForPrisma.prisma ?? new PrismaClient();
if (process.env.NODE_ENV !== 'production') {
globalForPrisma.prisma = prisma;
}

SvelteKit load functions run on the server, making them the right place to query DB9.

src/routes/todos/+page.server.ts
import { prisma } from '$lib/server/prisma';
import type { PageServerLoad } from './$types';
export const load: PageServerLoad = async () => {
const todos = await prisma.todo.findMany({
orderBy: { createdAt: 'desc' },
});
return { todos };
};

SvelteKit form actions handle mutations server-side with progressive enhancement.

src/routes/todos/+page.server.ts
import { prisma } from '$lib/server/prisma';
import type { Actions, PageServerLoad } from './$types';
export const load: PageServerLoad = async () => {
const todos = await prisma.todo.findMany({
orderBy: { createdAt: 'desc' },
});
return { todos };
};
export const actions: Actions = {
create: async ({ request }) => {
const data = await request.formData();
await prisma.todo.create({
data: { title: data.get('title') as string },
});
},
toggle: async ({ request }) => {
const data = await request.formData();
const id = Number(data.get('id'));
const todo = await prisma.todo.findUniqueOrThrow({ where: { id } });
await prisma.todo.update({
where: { id },
data: { done: !todo.done },
});
},
};
  • Server-side only: All DB9 queries must run in load functions, form actions, or +server.ts API routes. Files in src/lib/server/ are automatically excluded from client bundles.
  • Host and port: pg.db9.io:5433 and the pg.d0000.db9.io:5432 that db9 create prints reach the same server; port 5432 works on either host.
  • TLS: Use sslmode=require in the connection string for DB9’s hosted service.
  • Node adapter required: DB9 requires a TCP pgwire connection. Use @sveltejs/adapter-node for deployment — edge adapters (Cloudflare, Vercel Edge) do not support raw TCP sockets.

Install the Node adapter:

Terminal
npm install @sveltejs/adapter-node
svelte.config.js
import adapter from '@sveltejs/adapter-node';
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
export default {
preprocess: vitePreprocess(),
kit: {
adapter: adapter(),
},
};

DB9 accepts port 5432 as well as 5433, so the client is not reaching DB9 at all. A refusal from localhost or 127.0.0.1 means the connection string never reached the driver — check that DATABASE_URL is set in the process that opens the connection.

DB9 requires pgwire (TCP), which is not available in edge runtimes. Switch to @sveltejs/adapter-node. Cloudflare and Vercel Edge adapters will not work.

SvelteKit’s dev server reloads modules on change. Without the singleton pattern shown above, each reload opens a new connection pool. Use the globalThis pattern in src/lib/server/ and restart the dev server to release stale connections.

prisma db push — used above — works for the initial schema creation against an empty database. Do not run it again once the tables exist: Prisma does not recognise DB9’s spelling of a serial column’s default and plans to recreate the sequence behind each autoincrement() id. For a table named after its model, as here, that second push reports success while pointing id at a new sequence that starts at 1, so inserts into a table that already has rows fail with 23505 duplicate key value. prisma migrate dev additionally fails at P3014, because DB9 rejects the hyphenated name Prisma gives its shadow database.

Create the initial schema with prisma db push, then apply later changes with raw SQL. See A second db push fails on autoincrement() ids for the details.