Nuxt
Nuxt 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 set up a Nuxt 3 app with DB9 using Prisma and Drizzle.
Prerequisites
Section titled “Prerequisites”- A DB9 database (create one)
- Node.js 18+
- Nuxt 3+
Create a DB9 Database
Section titled “Create a DB9 Database”db9 create --name nuxt-appGet the connection string:
db9 db status nuxt-appSet the connection string as an environment variable:
DATABASE_URL="postgresql://nuxt-app.admin:YOUR_PASSWORD@pg.db9.io:5433/postgres?sslmode=require"npx nuxi@latest init nuxt-db9cd nuxt-db9npm install prisma @prisma/clientnpx prisma initDefine your schema:
generator client { provider = "prisma-client-js"}
datasource db { provider = "postgresql" url = env("DATABASE_URL")}
model User { id Int @id @default(autoincrement()) email String @unique name String createdAt DateTime @default(now())}Push the schema and generate the client:
npx prisma db pushnpx prisma generateCreate a server utility to share the Prisma instance:
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
export default prisma;npx nuxi@latest init nuxt-db9cd nuxt-db9npm install drizzle-orm pgnpm install -D drizzle-kit @types/pgDefine your schema:
import { pgTable, serial, varchar, timestamp } from 'drizzle-orm/pg-core';
export const users = pgTable('users', { id: serial('id').primaryKey(), email: varchar('email', { length: 255 }).unique().notNull(), name: varchar('name', { length: 100 }).notNull(), createdAt: timestamp('created_at', { withTimezone: true }).defaultNow(),});Create a server utility for the database connection:
import { drizzle } from 'drizzle-orm/node-postgres';import { Pool } from 'pg';
const pool = new Pool({ connectionString: process.env.DATABASE_URL,});
export const db = drizzle(pool);Server API Route
Section titled “Server API Route”import prisma from '~/server/utils/prisma';
export default defineEventHandler(async () => { return await prisma.user.findMany({ orderBy: { createdAt: 'desc' }, });});import prisma from '~/server/utils/prisma';
export default defineEventHandler(async (event) => { const body = await readBody(event); return await prisma.user.create({ data: { email: body.email, name: body.name, }, });});import { db } from '~/server/utils/db';import { users } from '~/server/db/schema';
export default defineEventHandler(async () => { return await db.select().from(users).orderBy(users.createdAt);});import { db } from '~/server/utils/db';import { users } from '~/server/db/schema';
export default defineEventHandler(async (event) => { const body = await readBody(event); const result = await db.insert(users).values({ email: body.email, name: body.name, }).returning(); return result[0];});Composable Usage
Section titled “Composable Usage”Call the API routes from a page using useFetch:
<script setup lang="ts">const { data: users, refresh } = await useFetch('/api/users');
async function addUser() { await $fetch('/api/users', { method: 'POST', body: { name: 'Alice', email: 'alice@example.com' }, }); await refresh();}</script>
<template> <div> <button @click="addUser">Add User</button> <ul> <li v-for="user in users" :key="user.id"> {{ user.name }} ({{ user.email }}) </li> </ul> </div></template>Production Notes
Section titled “Production Notes”- Server-side only: All database access happens in the
server/directory. Nuxt server routes and utilities never ship to the client, so your connection string stays private. - Port 5433: DB9 uses port 5433, not the default PostgreSQL port 5432. Verify your
DATABASE_URLincludes the correct port. - TLS required: Always include
sslmode=requirein your connection string for DB9’s hosted service. - Nitro server preset: Use the Node.js preset for deployment. Edge presets (Cloudflare Workers, Deno Deploy) do not support raw TCP connections required by pgwire. Set
nitro.presetinnuxt.config.ts:
export default defineNuxtConfig({ nitro: { preset: 'node-server', },});Troubleshooting
Section titled “Troubleshooting”ECONNREFUSED on port 5432
Section titled “ECONNREFUSED on port 5432”DB9 uses port 5433, not 5432. Check that your DATABASE_URL includes :5433.
Edge preset not supported
Section titled “Edge preset not supported”DB9 requires pgwire (TCP), which is not available in edge runtimes. Switch to the node-server or node-cluster Nitro preset.
Environment variables not available in Nitro
Section titled “Environment variables not available in Nitro”Nitro reads .env files automatically in development. For production, set DATABASE_URL through your hosting platform’s environment variable configuration. If needed, add runtimeConfig in nuxt.config.ts:
export default defineNuxtConfig({ runtimeConfig: { databaseUrl: process.env.DATABASE_URL, },});Then access it in server routes with useRuntimeConfig().databaseUrl.
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