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:<name>- Database name (required)--location <hint>- Primary location hintweur- Western Europeeeur- Eastern Europeapac- Asia Pacificoc- Oceaniawnam- Western North Americaenam- Eastern North America
--jurisdiction- Data residency jurisdictioneu- European Unionfedramp- FedRAMP-compliant data centers
--update-config- Automatically add to wrangler.json--name <binding>- Custom binding name
wrangler.json
List Databases
View all D1 databases in your account:Get Database Info
Retrieve detailed information about a database:Delete a Database
Delete a D1 database and all its data:<database>- Database name or binding--skip-confirmation, -y- Skip confirmation prompt
Executing SQL
- Command
- File
Execute SQL Commands
Run SQL directly from the command line:<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
Data Management
Export Data
Export database data to a SQL file:<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
Insights and Analytics
View database insights and query analytics:- Query performance metrics
- Database size and growth
- Most expensive queries
- Query frequency
Migrations
Create Migration
Generate a new migration file:migrations/0001_add_users_table.sql
List Migrations
View migration status:Apply Migrations
Apply pending migrations:Time Travel
Restore database to a previous state:<database>- Database name or binding--bookmark- Bookmark/timestamp to restore to--json- JSON output
Worker Integration
Query D1 from your Worker:Local Development
Setup Local Database
D1 automatically creates local databases when you run:.wrangler/state/v3/d1/.
Execute Locally
Preview Database
Use a separate preview database for testing:wrangler.json
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
Related Commands
- KV Storage - Key-value storage
- R2 Storage - Object storage
- Durable Objects - Stateful coordination