clone

a daily shikaku puzzle for phones: one seeded puzzle per date, solved by dragging rectangles

main
rect.run / .github / skills / deno-deploy / references / DATABASES.md
DATABASES.md 3.8K rendered
1# Databases on Deno Deploy23## Overview45Deno 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.67## Available Database Engines89| Engine | Description |10|--------|-------------|11| **Deno KV** | Fast, globally distributed key-value store hosted by Deno |12| **PostgreSQL** | Connect your own PostgreSQL or provision managed Postgres via Prisma |1314## Key Concept: Timelines1516Deno Deploy automatically creates **isolated databases for each environment**:1718- **Production:** `{app-id}-production`19- **Git branches:** `{app-id}--{branch-name}`20- **Preview deployments:** `{app-id}-preview`2122This means your preview deployments won't accidentally modify production data.2324## Database CLI Commands2526Use `deno deploy database` to manage databases from the command line.2728### List Databases2930```bash31# List all databases in your organization32deno deploy database list3334# Search for databases by name35deno deploy database list my-prefix36```3738### Provision a Database3940Create a new managed database:4142```bash43# Provision a new Deno KV database44deno deploy database provision my-database --kind denokv4546# Provision a new Prisma PostgreSQL database (requires --region)47deno deploy database provision my-database --kind prisma --region us-east-148```4950### Link an External Database5152Link an existing external postgres database by providing a connection string:5354```bash55deno deploy database link my-database "postgres://user:pass@host:5432/db"56```5758### Assign / Detach5960Connect or disconnect a database from an app:6162```bash63# Assign a database to an app64deno deploy database assign my-database --app my-app6566# Detach a database from an app67deno deploy database detach my-database --app my-app68```6970Deno Deploy creates separate databases for each timeline automatically.7172### Query a Database7374Run queries directly from the CLI:7576```bash77deno deploy database query my-database production "SELECT * FROM users LIMIT 10"78```7980The second argument is the timeline name (e.g., `production`, `preview`, or a branch name), 81which can be found in the output of `deno deploy database list`.8283### Delete a Database8485```bash86deno deploy database delete my-database87```8889## Connecting in Code9091### Deno KV9293No configuration needed - just call `Deno.openKv()`:9495```typescript96const kv = await Deno.openKv();9798// Deno Deploy automatically connects to the right database99// based on your current environment (production, preview, etc.)100```101102### PostgreSQL103104Deno Deploy injects standard environment variables that most PostgreSQL libraries detect automatically:105106- `DATABASE_URL` - Full connection string107- `PGHOST`, `PGPORT`, `PGDATABASE`, `PGUSER`, `PGPASSWORD` - Individual components108109```typescript110// Recommended: npm:pg (best PostgreSQL driver for Deno Deploy)111import pg from "npm:pg";112const pool = new pg.Pool(); // Reads DATABASE_URL from environment automatically113const { rows } = await pool.query("SELECT * FROM users");114```115116## Local Development117118### With Tunnel119120Use `--tunnel` to connect your local dev server to your hosted development database:121122```bash123deno task --tunnel dev124```125126This gives you access to the same database environment variables locally.127128### Without Tunnel129130- **Deno KV:** Data stays in memory during local development131- **PostgreSQL:** Point to a local PostgreSQL instance or use the tunnel132133## Migrations134135Deno Deploy supports pre-deploy commands that run before each deployment. Use these for database migrations:136137```json138{139 "deploy": {140 "preDeploy": ["deno task db:migrate"]141 }142}143```144145## Sharing Databases146147Multiple apps can share the same database instance. Each app gets its own isolated databases per timeline, even when sharing.148149## Documentation150151- Databases overview: https://docs.deno.com/deploy/reference/databases/152- Deno KV reference: https://docs.deno.com/deploy/reference/deno_kv/