Skip to main content

Overview

Cloudflare D1 is a serverless SQL database built on SQLite. It provides a familiar SQL interface with global distribution and automatic replication.

Database Management

Create a Database

Create a new D1 database:
Options:
  • <name> - Database name (required)
  • --location <hint> - Primary location hint
    • weur - Western Europe
    • eeur - Eastern Europe
    • apac - Asia Pacific
    • oc - Oceania
    • wnam - Western North America
    • enam - Eastern North America
  • --jurisdiction - Data residency jurisdiction
    • eu - European Union
    • fedramp - FedRAMP-compliant data centers
  • --update-config - Automatically add to wrangler.json
  • --name <binding> - Custom binding name
Examples:
Configuration: Add to your wrangler.json:
wrangler.json

List Databases

View all D1 databases in your account:
Example Output:

Get Database Info

Retrieve detailed information about a database:
Example:

Delete a Database

Delete a D1 database and all its data:
Options:
  • <database> - Database name or binding
  • --skip-confirmation, -y - Skip confirmation prompt
This permanently deletes the database and all data. This action cannot be undone.

Executing SQL

Execute SQL Commands

Run SQL directly from the command line:
Options:
  • <database> - Database name or binding
  • --command - SQL query or queries (separated by ;)
  • --local - Execute against local database
  • --remote - Execute against remote database
  • --preview - Execute against preview database
  • --persist-to <dir> - Local persistence directory (with --local)
  • --json - Output as JSON
  • -y, --yes - Skip confirmation prompts
Examples:

Data Management

Export Data

Export database data to a SQL file:
Options:
  • <database> - Database name or binding
  • --output - Output file path (default: <database>.sql)
  • --local - Export from local database
  • --remote - Export from remote database
  • --no-schema - Exclude schema (data only)
  • --no-data - Exclude data (schema only)
  • --table <name> - Export specific table
Examples:

Insights and Analytics

View database insights and query analytics:
Shows:
  • Query performance metrics
  • Database size and growth
  • Most expensive queries
  • Query frequency

Migrations

Create Migration

Generate a new migration file:
Example:
Creates: migrations/0001_add_users_table.sql

List Migrations

View migration status:

Apply Migrations

Apply pending migrations:
Examples:

Time Travel

Restore database to a previous state:
Options:
  • <database> - Database name or binding
  • --bookmark - Bookmark/timestamp to restore to
  • --json - JSON output
Get current bookmark:

Worker Integration

Query D1 from your Worker:

Local Development

Setup Local Database

D1 automatically creates local databases when you run:
Data is stored in .wrangler/state/v3/d1/.

Execute Locally

Preview Database

Use a separate preview database for testing:
wrangler.json
Execute against preview:

Best Practices

Schema Design

  • Use migrations for schema changes
  • Add indexes for frequently queried columns
  • Use AUTOINCREMENT for primary keys
  • Define foreign keys for relationships

Query Optimization

  • Use prepared statements (prevents SQL injection)
  • Batch multiple queries together
  • Add indexes to improve read performance
  • Use EXPLAIN to analyze query plans

Development Workflow

  • Test queries locally with --local
  • Use preview databases for staging
  • Apply migrations to remote incrementally
  • Export backups regularly

Data Management

  • Use transactions (batch) for atomic operations
  • Implement proper error handling
  • Monitor database size and performance
  • Use Time Travel for point-in-time recovery

SQL Support

D1 supports SQLite syntax:
  • Standard SQL operations (SELECT, INSERT, UPDATE, DELETE)
  • JOINs (INNER, LEFT, RIGHT, FULL)
  • Aggregate functions (COUNT, SUM, AVG, MIN, MAX)
  • Window functions
  • CTEs (Common Table Expressions)
  • Indexes and constraints
  • Triggers and views

Limits

  • Database size: 10 GB (paid plans)
  • Query execution time: 30 seconds
  • Rows per query: 100,000
  • Batch size: 1,000 statements