Drizzle_
Use Drizzle ORM with an Appwrite native PostgreSQL database. Configure the driver, run migrations against the direct connection, and pool runtime traffic from serverless environments.
4 min read
Appwrite's native PostgreSQL database is a standard PostgreSQL engine, so Drizzle ORM works against it with no Appwrite-specific configuration. Point Drizzle's driver at the connection string from the Connections page and use Drizzle Kit, the query builder, and the rest of the toolchain as you would against any PostgreSQL server.
You'll need a native PostgreSQL database in a ready state and its credentials. See native PostgreSQL databases to create one and Connections to retrieve the connection string. The primary user is admin, and the database name is generated per database.
Set the connection string
In the Console, open your database and click Credentials. Copy the connection string from the DSN, .env, or Drizzle tab, or fetch it with postgresql.get(). Put it in your environment, never commit it:
DATABASE_URL="postgresql://admin:<password>@db-<hash>.<region>.appwrite.center:5432/<database>?sslmode=require"The TLS parameter (sslmode=require) is part of the connection string Appwrite returns. Appwrite Cloud terminates TLS at the edge and forwards traffic to your database over the internal network, so no extra certificate configuration is needed. For full certificate verification (verify-full) or mTLS, see Network security.
Install and configure the driver
Drizzle talks to PostgreSQL through one of two driver packages. Pick the one already in your stack:
# node-postgresnpm install drizzle-orm pgnpm install -D drizzle-kit @types/pg
# or postgres.jsnpm install drizzle-orm postgresnpm install -D drizzle-kitWith node-postgres, import drizzle from drizzle-orm/node-postgres and hand it the connection string:
import { drizzle } from 'drizzle-orm/node-postgres';
export const db = drizzle(process.env.DATABASE_URL!);With postgres.js, import drizzle from drizzle-orm/postgres-js. For a direct connection to the engine you can pass the URL straight through:
import { drizzle } from 'drizzle-orm/postgres-js';
export const db = drizzle(process.env.DATABASE_URL!);When you pool through the connection pooler in transaction mode (see Pool connections from serverless), postgres.js must disable prepared statements. Build the client yourself with prepare: false and pass it to drizzle:
import { drizzle } from 'drizzle-orm/postgres-js';import postgres from 'postgres';
const client = postgres(process.env.DATABASE_URL!, { prepare: false });
export const db = drizzle({ client });Define your tables in a schema file Drizzle Kit can read:
import { pgTable, serial, text, timestamp } from 'drizzle-orm/pg-core';
export const users = pgTable('users', { id: serial('id').primaryKey(), email: text('email').notNull().unique(), createdAt: timestamp('created_at').notNull().defaultNow()});Configure Drizzle Kit
Drizzle Kit reads drizzle.config.ts for migrations and introspection. Set the dialect, point schema at your table definitions, and pass the connection string through dbCredentials:
import { defineConfig } from 'drizzle-kit';
export default defineConfig({ dialect: 'postgresql', schema: './src/schema.ts', out: './drizzle', dbCredentials: { url: process.env.DATABASE_URL! }});Run migrations
Generate SQL migration files from your schema, then apply them:
npx drizzle-kit generatenpx drizzle-kit migrategenerate diffs your schema against the last snapshot and writes a timestamped .sql file into the out directory; migrate applies any pending files to the database. Point Drizzle Kit at the direct engine port (5432), not the pooler, because migrations issue DDL that needs a session-level connection. The primary admin user owns the generated database and can run schema changes. Use narrower database roles for application traffic that does not need DDL privileges.
To apply migrations from your application at startup instead of the CLI, use the matching migrate helper for your driver:
import { migrate } from 'drizzle-orm/node-postgres/migrator';import { db } from './db';
await migrate(db, { migrationsFolder: './drizzle' });Query with Drizzle
Once the schema is migrated, use the query builder:
import { desc } from 'drizzle-orm';import { db } from './db';import { users } from './schema';
const [user] = await db .insert(users) .values({ email: 'ada@example.com' }) .returning();
const recent = await db .select() .from(users) .orderBy(desc(users.createdAt)) .limit(10);On long-running servers, create the db instance once at module scope and reuse it. On serverless, keep a single instance per module scope so warm invocations reuse it, and rely on the pooler to absorb cold-start connection churn.
Pool connections from serverless
Each running instance opens its own connections to the engine. On serverless and edge platforms (Vercel, Netlify, Cloudflare), short-lived instances can fan out into more backend connections than the engine allows. Route runtime traffic through the connection pooler by connecting on the pooler port (6432) on the same hostname.
The pooler defaults to transaction mode, which does not keep a backend connection across statements, so server-side prepared statements are unavailable. Keep drizzle.config.ts and the startup migrator pointed at DIRECT_URL so DDL still runs over a session-level connection:
# Runtime: pooled, transaction modeDATABASE_URL="postgresql://admin:<password>@db-<hash>.<region>.appwrite.center:6432/<database>?sslmode=require"
# Migrations & introspection: direct connection to the engineDIRECT_URL="postgresql://admin:<password>@db-<hash>.<region>.appwrite.center:5432/<database>?sslmode=require"With postgres.js, disable prepared statements by building the client with prepare: false, as shown in Install and configure the driver.
import { defineConfig } from 'drizzle-kit';
export default defineConfig({ dialect: 'postgresql', schema: './src/schema.ts', out: './drizzle', dbCredentials: { url: process.env.DIRECT_URL! }});If your application relies on prepared statements, advisory locks, LISTEN/NOTIFY, temporary tables, or SET LOCAL, switch the pooler to session mode and remove prepare: false with postgres.js. See the pooler page for the trade-offs.
Use a branch for previews and CI
Branches are isolated copies of a database with their own hostname and connection string. They're ideal for running migrations against throwaway data in a pull-request preview or an integration-test job:
- Create a branch from the API and read its
connectionString. - Export it as
DIRECT_URL(and the pooled variant asDATABASE_URL). - Run
drizzle-kit migrateand your test suite against the branch. - Delete the branch when the job finishes.
Because a branch starts from a storage snapshot, the schema and data match the source database at branch time, so migrations run against realistic data without touching production.
Related
Connections
Retrieve credentials, rotate the password, and create database roles.
Connection pooler
Pool modes, ports, and read/write splitting for serverless workloads.
Branches
Ephemeral database copies for preview environments and CI.
Network security
TLS modes, certificate verification, mTLS, and IP allowlists.
Was this page helpful?
Share what worked or what we should fix. Once approved, our agents automatically apply suggested updates to the docs.