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.
Prerequisites
Section titled “Prerequisites”- A DB9 database (create one)
- Node.js 18+
- SvelteKit 2+
Create a DB9 Database
Section titled “Create a DB9 Database”db9 create --name sveltekit-appGet the connection string:
db9 db status sveltekit-appSet the connection string as an environment variable:
DATABASE_URL="postgresql://sveltekit-app.admin:YOUR_PASSWORD@pg.db9.io:5433/postgres?sslmode=require"Install
Section titled “Install”npx sv create sveltekit-db9cd sveltekit-db9npm install prisma @prisma/clientnpx prisma initSchema
Section titled “Schema”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:
npx prisma db pushGenerate the client:
npx prisma generateSingleton client
Section titled “Singleton client”Create a shared Prisma instance to avoid connection exhaustion during development:
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;}Install
Section titled “Install”npx sv create sveltekit-db9cd sveltekit-db9npm install drizzle-orm pgnpm install -D drizzle-kit @types/pgSchema
Section titled “Schema”import { pgTable, serial, varchar, boolean, timestamp } from 'drizzle-orm/pg-core';
export const todos = pgTable('todos', { id: serial('id').primaryKey(), title: varchar('title', { length: 500 }).notNull(), done: boolean('done').default(false), createdAt: timestamp('created_at', { withTimezone: true }).defaultNow(),});Singleton client
Section titled “Singleton client”import { drizzle } from 'drizzle-orm/node-postgres';import { Pool } from 'pg';
const globalForDb = globalThis as unknown as { pool: Pool };
const pool = globalForDb.pool ?? new Pool({ connectionString: process.env.DATABASE_URL,});
if (process.env.NODE_ENV !== 'production') { globalForDb.pool = pool;}
export const db = drizzle(pool);Push the schema to DB9:
npx drizzle-kit pushServer Load Function
Section titled “Server Load Function”SvelteKit load functions run on the server, making them the right place to query DB9.
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 };};import { db } from '$lib/server/db';import { todos } from '$lib/server/schema';import { desc } from 'drizzle-orm';import type { PageServerLoad } from './$types';
export const load: PageServerLoad = async () => { const allTodos = await db.select().from(todos).orderBy(desc(todos.createdAt));
return { todos: allTodos };};Form Actions
Section titled “Form Actions”SvelteKit form actions handle mutations server-side with progressive enhancement.
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 }, }); },};import { db } from '$lib/server/db';import { todos } from '$lib/server/schema';import { eq, desc, not } from 'drizzle-orm';import type { Actions, PageServerLoad } from './$types';
export const load: PageServerLoad = async () => { const allTodos = await db.select().from(todos).orderBy(desc(todos.createdAt)); return { todos: allTodos };};
export const actions: Actions = { create: async ({ request }) => { const data = await request.formData(); await db.insert(todos).values({ title: data.get('title') as string, }); }, toggle: async ({ request }) => { const data = await request.formData(); const id = Number(data.get('id')); await db .update(todos) .set({ done: not(todos.done) }) .where(eq(todos.id, id)); },};Production Notes
Section titled “Production Notes”- Server-side only: All DB9 queries must run in load functions, form actions, or
+server.tsAPI routes. Files insrc/lib/server/are automatically excluded from client bundles. - Port 5433: DB9 uses port 5433, not the default PostgreSQL port 5432.
- TLS required: Use
sslmode=requirein the connection string for DB9’s hosted service. - Node adapter required: DB9 requires a TCP pgwire connection. Use
@sveltejs/adapter-nodefor deployment — edge adapters (Cloudflare, Vercel Edge) do not support raw TCP sockets.
Install the Node adapter:
npm install @sveltejs/adapter-nodeimport adapter from '@sveltejs/adapter-node';import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
export default { preprocess: vitePreprocess(), kit: { adapter: adapter(), },};Troubleshooting
Section titled “Troubleshooting”ECONNREFUSED on port 5432
Section titled “ECONNREFUSED on port 5432”DB9 uses port 5433, not 5432. Verify your DATABASE_URL includes the correct port.
Edge adapter errors
Section titled “Edge adapter errors”DB9 requires pgwire (TCP), which is not available in edge runtimes. Switch to @sveltejs/adapter-node. Cloudflare and Vercel Edge adapters will not work.
Connection pool exhaustion in development
Section titled “Connection pool exhaustion in development”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 fails
Section titled “prisma db push fails”DB9 has limited information_schema support. If prisma db push fails, manage tables with raw SQL. See the Prisma guide for details.
Next Pages
Section titled “Next Pages”- Prisma — full Prisma integration guide
- Drizzle — full Drizzle integration guide
- Connect — connection strings and authentication
- Production Checklist — deployment readiness