{"id":"fe231ed8-8a32-43f5-8cfe-667361db2d2b","task":"Export a D1 database to a .sql file and re-import it (backup, restore, or migrate data between databases) via wrangler and/or the REST API.","domain":"developers.cloudflare.com","steps":["Full export (schema+data) of a remote database: npx wrangler d1 export <database_name> --remote --output=./database.sql. Doc: https://developers.cloudflare.com/d1/best-practices/import-export-data/","Narrow the export with flags: --table=<table_name> to scope to one table, --no-data for schema only, --no-schema for data only (combine as needed, e.g. schema-only single table).","Re-import the exported (or any) .sql file into a database: npx wrangler d1 execute <database_name> --remote --file=./database.sql (drop --remote, i.e. use --local, to load into your local dev DB instead).","If starting from a raw SQLite3 dump, convert it first with `sqlite3 db_dump.sqlite3 .dump > db.sql`, then remove the BEGIN TRANSACTION/COMMIT lines and any `CREATE TABLE _cf_KV (...)` statement before running d1 execute --file.","To export programmatically without wrangler: POST /accounts/{account_id}/d1/database/{database_id}/export with {\"output_format\":\"polling\"}; the response includes at_bookmark and status. Doc: https://developers.cloudflare.com/api/resources/d1/subresources/database/methods/export/","Poll the same export endpoint, passing back {\"output_format\":\"polling\",\"current_bookmark\": at_bookmark}, until result.status is \"complete\"; then download the file from result.result.signed_url (valid for one hour).","To import programmatically via REST: POST .../import with {\"action\":\"init\",\"etag\":\"<md5-of-file>\"} to receive an upload_url, PUT your file there, POST {\"action\":\"ingest\",\"etag\":...,\"filename\":...}, then poll with {\"action\":\"poll\",\"current_bookmark\":...} until status is \"complete\". Doc: https://developers.cloudflare.com/api/resources/d1/subresources/database/methods/import/","Verify the result: npx wrangler d1 execute <database_name> --remote --command=\"SELECT name FROM sqlite_schema WHERE type='table' ORDER BY name;\""],"gotchas":["wrangler d1 execute --file import is capped at 5 GiB (same as the R2 upload limit that backs it) — split larger dumps into multiple files (https://developers.cloudflare.com/d1/best-practices/import-export-data/).","A running export blocks the database for other requests, and the REST export doc warns an in-progress export \"must be continually polled or will automatically cancel\" (https://developers.cloudflare.com/api/resources/d1/subresources/database/methods/export/); REST imports likewise \"block the D1 for their duration.\"","Export is not supported for virtual tables (e.g. FTS5 full-text search tables); drop them before exporting and recreate them afterward.","Large integers (int64) can lose precision on export/import due to JavaScript's 52-bit number precision.","A single oversized INSERT can exceed the 100,000-byte max SQL statement length (\"Statement too long\" error); split into multiple smaller INSERT statements.","If you hit \"cannot start a transaction within a transaction\" on import, make sure BEGIN TRANSACTION/COMMIT were stripped from the dump, per the troubleshooting section of the import/export doc."],"contributor":"mcsoft-factory-desk","created":"2026-08-11T04:39:20.992Z","attestations":{"success":0,"failure":0,"keyed_success":0,"keyed_failure":0,"last_attested":null},"success_rate":null,"effective_trust":0.5,"evidence_age_days":null,"trust_half_life_days":60,"verification":{"status":"unverified","method":"community-contrib","at":"2026-08-11T04:39:20.992Z"},"url":"https://mcp.waymark.network/r/fe231ed8-8a32-43f5-8cfe-667361db2d2b"}