# Cloudflare D1 URL: /docs/features/database/cloudflare-d1 Learn how to setup and use Cloudflare D1 and Drizzle ORM. ## Setup If you're using the [Cloudflare Monorepo](/docs/picking-a-template/cloudflare-monorepo) template, the `@workspace/drizzle` package is setup with [Cloudflare D1](https://www.cloudflare.com/developer-platform/products/d1/) and Drizzle ORM out of the box. To use D1, you will need to create a new D1 database for your project, update your wrangler config files to include your D1 database, then run migrations. ### Create a new D1 database In the root of your project, run the following command to create a new D1 database (you can replace `my_example_db` with your desired database name): ```bash title="/" pnpm wrangler d1 create my_example_db ``` This will create a new D1 database and output the database ID, like so: ```bash title="/" > wrangler d1 create my_example_db ⛅️ wrangler 4.25.0 ─────────────────── ✅ Successfully created DB 'my_example_db' in region WNAM Created your new D1 database. { "d1_databases": [ { "binding": "DB", "database_name": "my_example_db", "database_id": "49d82112-f513-458a-bfcd-361a2434435e" } ] } ``` If you are using staging environments, you will need to create separate D1 databases for both staging and production. You can do this by running the above command with different database names. ### Update wrangler config files Next, you will need to update all `wranger.jsonc` files to point to your new D1 database(s). Open the `wrangler.jsonc` files in the following locations: 1. `/apps/meter/wrangler.jsonc` 2. `/apps/realtime/wrangler.jsonc` 3. `/apps/web/wrangler.jsonc` 4. `/packages/drizzle/wrangler.jsonc` In each of these files, you will need to replace the contents of the `d1_databases` array with the database name and ID you just created. For example: ```jsonc title="/apps/meter/wrangler.jsonc" { // ... "d1_databases": [ { "binding": "DB", // Replace with your database name "database_name": "my_example_db", // Replace with your database ID "database_id": "49d82112-f513-458a-bfcd-361a2434435e", // ... } ], // ... } ``` ### Run migrations Lastly, you will need to ensure that your D1 database contains the latest schema. Since your D1 databases are new and empty, you can first delete the existing migrations in the `@workspace/drizzle` package: ```bash title="/packages/drizzle" rm -rf ./.drizzle ``` Then generate fresh migrations based on your current schema: ```bash title="/packages/drizzle" pnpm run migrate:generate ``` Finally, run the migrations to apply them to your local D1 database: ```bash title="/packages/drizzle" # Applies the migrations to your LOCAL D1 database pnpm run migrate:apply:local ``` You can verify that the migrations have been applied by checking the `migrations` table in your D1 database. You can do this by using Drizzle Studio via the following command: ```bash title="/packages/drizzle" pnpm run studio:local ``` Once you are ready to roll out your database changes to either your staging or production environments, you can run the following command to apply the migrations to your D1 database in the respective environment: ```bash title="/packages/drizzle" # Applies the migrations to your STAGING D1 database pnpm run migrate:apply:stage # Applies the migrations to your PRODUCTION D1 database pnpm run migrate:apply:prod ```