Skip to main content
Manage D1 databases, execute SQL commands, handle migrations, and work with Time Travel features.

Commands

Database Management

Create a new D1 database.
wrangler d1 create <name>
name
string
required
The name of the new D1 database
Options:
--location
string
A hint for the primary location of the new DB. Options:
  • weur: Western Europe
  • eeur: Eastern Europe
  • apac: Asia Pacific
  • oc: Oceania
  • wnam: Western North America
  • enam: Eastern North America
--jurisdiction
string
The location to restrict the D1 database to run and store data within to comply with local regulations. Options:
  • eu: The European Union
  • fedramp: FedRAMP-compliant data centers
--update-config
boolean
Automatically update your wrangler configuration file with the new database binding
--binding-name
string
The name of the binding to use in your wrangler configuration file
Example:
wrangler d1 create my-database
wrangler d1 create my-database --location=weur
wrangler d1 create eu-database --jurisdiction=eu
Note: If jurisdictions are set, the location hint is ignored.
List all D1 databases in your account.
wrangler d1 list
Options:
--json
boolean
default:"false"
Return output as clean JSON
Example:
wrangler d1 list
wrangler d1 list --json
Get information about a D1 database, including the current database size and state.
wrangler d1 info <name>
name
string
required
The name of the DB
Options:
--json
boolean
default:"false"
Return output as clean JSON
Example:
wrangler d1 info my-database
wrangler d1 info my-database --json
Shows database size, creation date, version, and 24-hour metrics (read/write queries and rows).
Delete a D1 database.
wrangler d1 delete <name>
name
string
required
The name or binding of the DB
Options:
--skip-confirmation
boolean
default:"false"
Skip confirmation (alias: -y)
Example:
wrangler d1 delete my-database
wrangler d1 delete my-database --skip-confirmation
This action is irreversible and will permanently delete all data in the database.

SQL Execution

Execute a SQL command or SQL file against a D1 database.
wrangler d1 execute <database>
database
string
required
The name or binding of the DB
Options:
--command
string
The SQL query you wish to execute, or multiple queries separated by ;
--file
string
A .sql file to ingest
--yes
boolean
Answer “yes” to any prompts (alias: -y)
--local
boolean
Execute commands/files against a local DB for use with wrangler dev
--remote
boolean
Execute commands/files against a remote D1 database
--preview
boolean
default:"false"
Execute commands/files against a preview D1 database
--persist-to
string
Specify directory to use for local persistence (for use with --local)
--json
boolean
default:"false"
Return output as clean JSON
Example:
wrangler d1 execute my-database --command="SELECT * FROM users"
wrangler d1 execute my-database --file=./schema.sql
wrangler d1 execute my-database --command="SELECT * FROM users" --remote
wrangler d1 execute my-database --file=./seed.sql --local
You must provide either --command or --file. By default, executes against local database unless --remote is specified.

Data Import/Export

Export the contents or schema of your database as a .sql file.
wrangler d1 export <name> --output <path>
name
string
required
The name of the D1 database to export
Options:
--output
string
required
Path to the SQL file for your export
--local
boolean
Export from your local DB you use with wrangler dev
--remote
boolean
Export from a remote D1 database
--table
string
Specify which tables to include in export (can be specified multiple times)
--no-schema
boolean
Only output table contents, not the DB schema
--no-data
boolean
Only output table schema, not the contents of the DBs themselves
Example:
wrangler d1 export my-database --output=backup.sql
wrangler d1 export my-database --output=backup.sql --remote
wrangler d1 export my-database --output=schema.sql --no-data
wrangler d1 export my-database --output=data.sql --table=users --table=posts
You cannot specify both --no-schema and --no-data.

Migrations

Create a new migration file.
wrangler d1 migrations create <database> <message>
database
string
required
The name or binding of the DB
message
string
required
The migration message (description)
Example:
wrangler d1 migrations create my-database "create users table"
wrangler d1 migrations create my-database "add email column"
This will generate a new versioned file inside the ‘migrations’ folder with format 0000_create_user_table.sql.
View a list of unapplied migration files.
wrangler d1 migrations list <database>
database
string
required
The name or binding of the DB
Options:
--local
boolean
Check migrations against a local DB for use with wrangler dev
--remote
boolean
Check migrations against a remote DB
--preview
boolean
default:"false"
Check migrations against a preview D1 DB
--persist-to
string
Specify directory to use for local persistence (you must use --local with this flag)
Example:
wrangler d1 migrations list my-database
wrangler d1 migrations list my-database --remote
Apply any unapplied D1 migrations.
wrangler d1 migrations apply <database>
database
string
required
The name or binding of the DB
Options:
--local
boolean
Execute commands/files against a local DB for use with wrangler dev
--remote
boolean
Execute commands/files against a remote DB
--preview
boolean
default:"false"
Execute commands/files against a preview D1 DB
--persist-to
string
Specify directory to use for local persistence (you must use --local with this flag)
Example:
wrangler d1 migrations apply my-database
wrangler d1 migrations apply my-database --remote
This command will prompt you to confirm the migrations you are about to apply. If applying a migration results in an error, it will be rolled back, and the previous successful migration will remain applied.

Time Travel

Retrieve information about a database at a specific point-in-time using Time Travel.
wrangler d1 time-travel info <database>
database
string
required
The name or binding of the DB
Options:
--timestamp
string
Accepts a Unix (seconds from epoch) or RFC3339 timestamp (e.g. 2023-07-13T08:46:42.228Z) to retrieve a bookmark for
--json
boolean
default:"false"
Return output as clean JSON
Example:
wrangler d1 time-travel info my-database
wrangler d1 time-travel info my-database --timestamp=2023-07-13T08:46:42.228Z
wrangler d1 time-travel info my-database --timestamp=1689240402
For more information about Time Travel, see the D1 Time Travel documentation.
Restore a database back to a specific point-in-time.
wrangler d1 time-travel restore <database>
database
string
required
The name or binding of the DB
Options:
--bookmark
string
Bookmark to use for time travel
--timestamp
string
Accepts a Unix (seconds from epoch) or RFC3339 timestamp (e.g. 2023-07-13T08:46:42.228Z) to retrieve a bookmark for (within the last 30 days)
--json
boolean
default:"false"
Return output as clean JSON
Example:
wrangler d1 time-travel restore my-database --bookmark=abc123
wrangler d1 time-travel restore my-database --timestamp=2023-07-13T08:46:42.228Z
You must provide either --timestamp or --bookmark, but not both. This will overwrite all data in the database and cancel in-flight queries and transactions.For more information about Time Travel, see the D1 Time Travel documentation.

Build docs developers (and LLMs) love