Skip to main content

database

Creates, updates, deletes, gets or lists a database resource.

Overview

Namedatabase
TypeResource
Idcloudflare.d1.database

Fields

The following fields are returned by SELECT queries:

Database details response

NameDatatypeDescription
namestringD1 database name. (example: my-database)
created_atstring (date-time)Specifies the timestamp the resource was created as an ISO8601 string. (example: 2022-11-15T18:25:44.442097Z)
file_sizenumberThe D1 database's size, in bytes.
jurisdictionstringSpecify the location to restrict the D1 database to run and store data. If this option is present, the location hint is ignored. (eu, fedramp) (example: eu)
num_tablesnumber
read_replicationobjectConfiguration for D1 read replication.
uuidstringD1 database identifier (UUID). (example: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)
versionstring (example: production)

Methods

The following methods are available for this resource:

NameAccessible byRequired ParamsOptional ParamsDescription
getselectaccount_id, database_idReturns the specified D1 database.
listselectaccount_idname, page, per_pageReturns a list of D1 databases.
createinsertaccount_id, nameReturns the created D1 database.
editupdateaccount_id, database_idUpdates partially the specified D1 database.
updatereplaceaccount_id, database_id, read_replicationUpdates the specified D1 database.
deletedeleteaccount_id, database_idDeletes the specified D1 database.
exportexecaccount_id, database_id, output_formatReturns a URL where the SQL contents of your D1 can be downloaded. Note: this process may take some time for larger DBs, during which your D1 will be unavailable to serve queries. To avoid blocking your DB unnecessarily, an in-progress export must be continually polled or will automatically cancel.
importexecaccount_id, database_id, actionGenerates a temporary URL for uploading an SQL file to, then instructing the D1 to import it and polling it for status updates. Imports block the D1 for their duration.
create_rawexecaccount_id, database_idReturns the query result rows as arrays rather than objects. This is a performance-optimized version of the /query endpoint.

Parameters

Parameters can be passed in the WHERE clause of a query. Check the Methods section to see which parameters are required or optional for each operation.

NameDatatypeDescription
account_idstringThe Cloudflare account ID.
database_idstringThe D1 database ID.
namestring
pagenumber
per_pagenumber

SELECT examples

Returns the specified D1 database.

SELECT
name,
created_at,
file_size,
jurisdiction,
num_tables,
read_replication,
uuid,
version
FROM cloudflare.d1.database
WHERE account_id = '{{ account_id }}' -- required
AND database_id = '{{ database_id }}' -- required
;

INSERT examples

Returns the created D1 database.

INSERT INTO cloudflare.d1.database (
jurisdiction,
name,
primary_location_hint,
account_id
)
SELECT
'{{ jurisdiction }}',
'{{ name }}' /* required */,
'{{ primary_location_hint }}',
'{{ account_id }}'
RETURNING
errors,
messages,
result,
success
;

UPDATE examples

Updates partially the specified D1 database.

UPDATE cloudflare.d1.database
SET
read_replication = '{{ read_replication }}'
WHERE
account_id = '{{ account_id }}' --required
AND database_id = '{{ database_id }}' --required
RETURNING
errors,
messages,
result,
success;

REPLACE examples

Updates the specified D1 database.

REPLACE cloudflare.d1.database
SET
read_replication = '{{ read_replication }}'
WHERE
account_id = '{{ account_id }}' --required
AND database_id = '{{ database_id }}' --required
AND read_replication = '{{ read_replication }}' --required
RETURNING
errors,
messages,
result,
success;

DELETE examples

Deletes the specified D1 database.

DELETE FROM cloudflare.d1.database
WHERE account_id = '{{ account_id }}' --required
AND database_id = '{{ database_id }}' --required
;

Lifecycle Methods

Returns a URL where the SQL contents of your D1 can be downloaded. Note: this process may take some time for larger DBs, during which your D1 will be unavailable to serve queries. To avoid blocking your DB unnecessarily, an in-progress export must be continually polled or will automatically cancel.

EXEC cloudflare.d1.database.export
@account_id='{{ account_id }}' --required,
@database_id='{{ database_id }}' --required
@@json=
'{
"current_bookmark": "{{ current_bookmark }}",
"dump_options": "{{ dump_options }}",
"output_format": "{{ output_format }}"
}'
;