D1
The easiest way to add SQL to your application
D1 is built into the Cloudflare Workers Platform with out-of-the-box integration with Workers. Applications need to save data. SQLite offers a familiar, relational database with SQL querying.
Familiar SQL at the Edge
Native Worker Integration
Scale for Vibe-Coding Platforms
Built for Data-Driven Applications

Global Read Replication
Automatically create read-only copies of your database across Cloudflare's global network, allowing you to serve data from a location near your users for incredibly fast read performance.

Native Worker Integration
Query your database with near-zero latency directly from your serverless functions, as D1 is built to be the stateful backend for the Cloudflare Workers ecosystem.

Modern ORM Support
Develop faster and with full type-safety by using your favorite Object-Relational Mappers, with first-class support for popular tools like Prisma and Drizzle ORM.

Time Travel Backups
Effortlessly restore your entire database to any minute within the last 30 days, providing powerful point-in-time recovery to protect against accidental data loss or corruption.

Generated Columns
Define columns that automatically compute their values from other columns or JSON data, simplifying your application logic by offloading transformations and calculations directly to the database.

Secure and easy Worker Bindings
Securely connect your D1 databases to your code with a simple binding in your configuration, streamlining both production deployments and local development with the Wrangler CLI.
Vibe-coding platforms
Familiar SQL at the Edge
User Profile & Configuration Storage
From schema to scale
Practical examples for building and scaling SQL-backed apps on the edge.
export default { async fetch(request, env) { // Create a table await env.DB.exec(`CREATE TABLE IF NOT EXISTS users ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, email TEXT UNIQUE NOT NULL, created_at DATETIME DEFAULT CURRENT_TIMESTAMP )`);
// Insert data const { results } = await env.DB.prepare( `INSERT INTO users (name, email) VALUES (?, ?)`, ) .bind('John Doe', 'john@example.com') .run();
// Query data const { results: users } = await env.DB.prepare( `SELECT * FROM users WHERE email = ?`, ) .bind('john@example.com') .all();
return new Response(JSON.stringify(users), { headers: { 'Content-Type': 'application/json' }, }); },};// schema.prismagenerator client { provider = "prisma-client-js"}
datasource db { provider = "sqlite" url = env("DATABASE_URL")}
model User { id Int @id @default(autoincrement()) name String email String @unique createdAt DateTime @default(now()) posts Post[]}
model Post { id Int @id @default(autoincrement()) title String content String authorId Int author User @relation(fields: [authorId], references: [id]) createdAt DateTime @default(now())}
// worker.tsimport { PrismaClient } from '@prisma/client';
export default { async fetch(request, env) { const prisma = new PrismaClient({ datasources: { db: { url: env.DATABASE_URL } } });
// Create a user with posts const user = await prisma.user.create({ data: { name: 'Jane Doe', email: 'jane@example.com', posts: { create: [ { title: 'Hello World', content: 'My first post!' }, { title: 'D1 is Great', content: 'I love using D1 with Prisma!' } ] } }, include: { posts: true } });
return new Response(JSON.stringify(user), { headers: { 'Content-Type': 'application/json' } }); }};export default { async fetch(request, env) { const { searchParams } = new URL(request.url); const userId = searchParams.get('userId');
if (!userId) { return new Response('Missing userId parameter', { status: 400 }); }
// Use read replica for better performance const dbSession = env.DB.withSession(); const { results } = await dbSession .prepare( `SELECT u.*, COUNT(p.id) as post_count FROM users u LEFT JOIN posts p ON u.id = p.authorId WHERE u.id = ? GROUP BY u.id`, ) .bind(userId) .all();
return new Response(JSON.stringify(results), { headers: { 'Content-Type': 'application/json' }, }); },};wrangler d1 time-travel restore YOUR_DATABASE --timestamp=UNIX_TIMESTAMP
🚧 Restoring database YOUR_DATABASE from bookmark 00000080-ffffffff-00004c60-390376cb1c4dd679b74a19d19f5ca5be
⚠️ This will overwrite all data in database YOUR_DATABASE.In-flight queries and transactions will be cancelled.
✔ OK to proceed (y/N) … yes⚡️ Time travel in progress...✅ Database YOUR_DATABASE restored back to bookmark 00000080-ffffffff-00004c60-390376cb1c4dd679b74a19d19f5ca5be
↩️ To undo this operation, you can restore to the previous bookmark: 00000085-ffffffff-00004c6d-2510c8b03a2eb2c48b2422bb3b33fad5 D1 Pricing
Serverless SQL that scales horizontally. View Storage & Data pricing details
Storage
5 GB (total)
$0.75 / GB-month
Rows Read
5 million / day
$0.001 / million rows
Rows Written
100,000 / day
$1.00 / million rows
SiteGPT
"
We use Cloudflare for everything – storage, cache, queues, and most importantly for training data and deploying the app on the edge, so I can ensure the product is reliable and fast. It's also been the most affordable option, with competitors costing more for a single day's worth of requests than Cloudflare costs in a month. "
Powerful primitives, seamlessly integrated
Built on systems powering 20% of the Internet, D1 runs on the same infrastructure Cloudflare uses to build Cloudflare. Enterprise-grade reliability, security, and performance are standard.
Build without boundaries
Join thousands of developers who've eliminated infrastructure complexity and deployed globally with Cloudflare. Start building for free — no credit card required.