Subchapter 1.2
references/DATABASES.mdMarkdown4 KBView on GitHub
Deno Deploy provides built-in database support with automatic environment isolation. You don’t need to manage connection strings or worry about mixing production and development data.
| Engine | Description |
|---|---|
| Deno KV | Fast, globally distributed key-value store hosted by Deno |
| PostgreSQL | Connect your own PostgreSQL or provision managed Postgres via Prisma |
Deno Deploy automatically creates isolated databases for each environment:
{app-id}-production{app-id}--{branch-name}{app-id}-previewThis means your preview deployments won’t accidentally modify production data.
Use deno deploy database to manage databases from the command line.
# List all databases in your organization
deno deploy database list
# Search for databases by name
deno deploy database list my-prefixCreate a new managed database:
# Provision a new Deno KV database
deno deploy database provision my-database --kind denokv
# Provision a new Prisma PostgreSQL database (requires --region)
deno deploy database provision my-database --kind prisma --region us-east-1Link an existing external postgres database by providing a connection string:
deno deploy database link my-database "postgres://user:pass@host:5432/db"Connect or disconnect a database from an app:
# Assign a database to an app
deno deploy database assign my-database --app my-app
# Detach a database from an app
deno deploy database detach my-database --app my-appDeno Deploy creates separate databases for each timeline automatically.
Run queries directly from the CLI:
deno deploy database query my-database production "SELECT * FROM users LIMIT 10"The second argument is the timeline name (e.g., production, preview, or a
branch name), which can be found in the output of deno deploy database list.
deno deploy database delete my-databaseNo configuration needed - just call Deno.openKv():
const kv = await Deno.openKv();
// Deno Deploy automatically connects to the right database
// based on your current environment (production, preview, etc.)Deno Deploy injects standard environment variables that most PostgreSQL libraries detect automatically:
DATABASE_URL - Full connection stringPGHOST, PGPORT, PGDATABASE, PGUSER, PGPASSWORD - Individual
components// Recommended: npm:pg (best PostgreSQL driver for Deno Deploy)
import pg from "npm:pg";
const pool = new pg.Pool(); // Reads DATABASE_URL from environment automatically
const { rows } = await pool.query("SELECT * FROM users");Use --tunnel to connect your local dev server to your hosted development
database:
deno task --tunnel devThis gives you access to the same database environment variables locally.
Deno Deploy supports pre-deploy commands that run before each deployment. Use these for database migrations:
{
"deploy": {
"preDeploy": ["deno task db:migrate"]
}
}Multiple apps can share the same database instance. Each app gets its own isolated databases per timeline, even when sharing.