← back

Using Drizzle Studio with a Local D1 Database

• 3 min read

If you're using Cloudflare D1 locally and want to open it in Drizzle Studio, you can do that easily with a small setup.

This guide shows you how to:

  • Find your local D1 database file.
  • Run Drizzle Studio on it.
  • Make sure your Drizzle config connects to the right database.

1. Find your local database file

When you run your Cloudflare D1 database locally (for example, with wrangler dev), Cloudflare creates a SQLite file in a hidden directory called .wrangler.

You don't have to find this file by hand — you can use a script that does it automatically.

export LOCAL_DB_PATH=$(find .wrangler/state/v3/d1/miniflare-D1DatabaseObject -type f -name '*.sqlite' -print0 | xargs -0 ls -t | head -1) && drizzle-kit studio --config=drizzle.config.ts

This command:

  • Looks inside .wrangler/state/v3/d1/ for your most recent .sqlite file.
  • Sets an environment variable called LOCAL_DB_PATH to that file.
  • Starts Drizzle Studio using your drizzle.config.ts file.

You can save this as a script in your package.json for convenience.

{
  "scripts": {
    "db:studio:dev": "export LOCAL_DB_PATH=$(find .wrangler/state/v3/d1/miniflare-D1DatabaseObject -type f -name '*.sqlite' -print0 | xargs -0 ls -t | head -1) && drizzle-kit studio --config=drizzle.config.ts"
  }
}

Now you can just run:

npm run db:studio:dev

2. Update your Drizzle config

You need to tell Drizzle where to find the local database file.

In your drizzle.config.ts, add this small check for the LOCAL_DB_PATH variable.

const LOCAL_DB_PATH = String(process.env.LOCAL_DB_PATH || '')

export default {
  schema: './src/schema.ts',
  out: './migrations',
  dialect: 'sqlite',
  ...(LOCAL_DB_PATH
    ? {
        dbCredentials: {
          url: LOCAL_DB_PATH,
        },
      }
    : undefined),
}

This means:

  • If LOCAL_DB_PATH is set (from the script), Drizzle connects to your local SQLite file.
  • If it's not set, Drizzle will skip that setting — so you can still use your normal remote config in production.

3. Open Drizzle Studio

Once everything's set up, run:

npm run db:studio:dev

Drizzle Studio will open and connect directly to your local Cloudflare D1 database. You can now inspect tables, query data, and test changes — all using your dev environment.

Alternatively...

You could just run this inside your project:

npx @outerbase/studio

Note: this will only work with a single d1 instance.

Handling multiple D1 databases

If you have multiple D1 databases in your wrangler.jsonc, the simple find command might pick the wrong one. You can make it more precise by reading the database IDs from your config.

# Get the first database ID from wrangler.jsonc
DB_ID=$(grep -A 1 '\[\[d1_databases\]\]' wrangler.jsonc | grep 'database_id' | head -1 | cut -d'"' -f2)

# Find the corresponding local database file
LOCAL_DB_PATH=$(find .wrangler/state/v3/d1/miniflare-D1DatabaseObject -name "*${DB_ID}*" -type f -name '*.sqlite' -print0 | xargs -0 ls -t | head -1)

# Start Drizzle Studio
export LOCAL_DB_PATH && drizzle-kit studio --config=drizzle.config.ts

Or if you want to be even more specific and pick a database by name:

# Get database ID by name (replace 'my-db' with your database name)
DB_ID=$(grep -A 2 'name = "my-db"' wrangler.jsonc | grep 'database_id' | cut -d'"' -f2)

# Find and start Drizzle Studio
LOCAL_DB_PATH=$(find .wrangler/state/v3/d1/miniflare-D1DatabaseObject -name "*${DB_ID}*" -type f -name '*.sqlite' -print0 | xargs -0 ls -t | head -1)
export LOCAL_DB_PATH && drizzle-kit studio --config=drizzle.config.ts

This way you're guaranteed to connect to the right database, even with multiple D1 instances in your project.

Testing the commands

Before running the full command, you can test each part to make sure it's working:

# 1. Check if you have a wrangler.jsonc file
ls -la wrangler.jsonc

# 2. See what databases are configured
grep -A 3 '\[\[d1_databases\]\]' wrangler.jsonc

# 3. Test extracting the first database ID
grep -A 1 '\[\[d1_databases\]\]' wrangler.jsonc | grep 'database_id' | head -1 | cut -d'"' -f2

# 4. Check if the local database directory exists
ls -la .wrangler/state/v3/d1/miniflare-D1DatabaseObject/

# 5. Test finding the database file (replace YOUR_DB_ID with actual ID)
find .wrangler/state/v3/d1/miniflare-D1DatabaseObject -name "*YOUR_DB_ID*" -type f -name '*.sqlite'

Run these commands in your project root (where your wrangler.jsonc file is located). If any step fails, you'll know exactly where the issue is.

Once you've verified each step works, you can run the full command or add it to your package.json scripts.