Skip to main content

databases

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

Overview

Namedatabases
TypeResource
Idgoogle.spanner.databases

Fields

The following fields are returned by SELECT queries:

Successful response

NameDatatypeDescription
namestringRequired. The name of the database. Values are of the form projects//instances//databases/, where `` is as specified in the CREATE DATABASE statement. This name can be passed to other API methods to identify the database.
createTimestring (google-datetime)Output only. If exists, the time at which the database creation started.
databaseDialectstringOutput only. The dialect of the Cloud Spanner Database.
defaultLeaderstringOutput only. The read-write region which contains the database's leader replicas. This is the same as the value of default_leader database option set using DatabaseAdmin.CreateDatabase or DatabaseAdmin.UpdateDatabaseDdl. If not explicitly set, this is empty.
earliestVersionTimestring (google-datetime)Output only. Earliest timestamp at which older versions of the data can be read. This value is continuously updated by Cloud Spanner and becomes stale the moment it is queried. If you are using this value to recover data, make sure to account for the time from the moment when the value is queried to the moment when you initiate the recovery.
enableDropProtectionbooleanOptional. Whether drop protection is enabled for this database. Defaults to false, if not set. For more details, please see how to prevent accidental database deletion.
encryptionConfigobjectOutput only. For databases that are using customer managed encryption, this field contains the encryption configuration for the database. For databases that are using Google default or other types of encryption, this field is empty. (id: EncryptionConfig)
encryptionInfoarrayOutput only. For databases that are using customer managed encryption, this field contains the encryption information for the database, such as all Cloud KMS key versions that are in use. The encryption_status field inside of each EncryptionInfo is not populated. For databases that are using Google default or other types of encryption, this field is empty. This field is propagated lazily from the backend. There might be a delay from when a key version is being used and when it appears in this field.
quorumInfoobjectOutput only. Applicable only for databases that use dual-region instance configurations. Contains information about the quorum. (id: QuorumInfo)
reconcilingbooleanOutput only. If true, the database is being updated. If false, there are no ongoing update operations for the database.
restoreInfoobjectOutput only. Applicable only for restored databases. Contains information about the restore source. (id: RestoreInfo)
statestringOutput only. The current database state.
versionRetentionPeriodstringOutput only. The period in which Cloud Spanner retains all versions of data for the database. This is the same as the value of version_retention_period database option set using UpdateDatabaseDdl. Defaults to 1 hour, if not set.

Methods

The following methods are available for this resource:

NameAccessible byRequired ParamsOptional ParamsDescription
projects_instances_databases_getselectprojectsId, instancesId, databasesIdGets the state of a Cloud Spanner database.
projects_instances_databases_listselectprojectsId, instancesIdpageSize, pageTokenLists Cloud Spanner databases.
projects_instances_databases_createinsertprojectsId, instancesIdCreates a new Spanner database and starts to prepare it for serving. The returned long-running operation will have a name of the format /operations/ and can be used to track preparation of the database. The metadata field type is CreateDatabaseMetadata. The response field type is Database, if successful.
projects_instances_databases_patchupdateprojectsId, instancesId, databasesIdupdateMaskUpdates a Cloud Spanner database. The returned long-running operation can be used to track the progress of updating the database. If the named database does not exist, returns NOT_FOUND. While the operation is pending: * The database's reconciling field is set to true. * Cancelling the operation is best-effort. If the cancellation succeeds, the operation metadata's cancel_time is set, the updates are reverted, and the operation terminates with a CANCELLED status. * New UpdateDatabase requests will return a FAILED_PRECONDITION error until the pending operation is done (returns successfully or with error). * Reading the database via the API continues to give the pre-request values. Upon completion of the returned operation: * The new values are in effect and readable via the API. * The database's reconciling field becomes false. The returned long-running operation will have a name of the format projects//instances//databases//operations/ and can be used to track the database modification. The metadata field type is UpdateDatabaseMetadata. The response field type is Database, if successful.
projects_instances_databases_drop_databasedeleteprojectsId, instancesId, databasesIdDrops (aka deletes) a Cloud Spanner database. Completed backups for the database will be retained according to their expire_time. Note: Cloud Spanner might continue to accept requests for a few seconds after the database has been deleted.
projects_instances_databases_changequorumexecprojectsId, instancesId, databasesIdChangeQuorum is strictly restricted to databases that use dual-region instance configurations. Initiates a background operation to change the quorum of a database from dual-region mode to single-region mode or vice versa. The returned long-running operation has a name of the format projects//instances//databases//operations/ and can be used to track execution of the ChangeQuorum. The metadata field type is ChangeQuorumMetadata. Authorization requires spanner.databases.changequorum permission on the resource database.
projects_instances_databases_restoreexecprojectsId, instancesIdCreate a new database by restoring from a completed backup. The new database must be in the same project and in an instance with the same instance configuration as the instance containing the backup. The returned database long-running operation has a name of the format projects//instances//databases//operations/, and can be used to track the progress of the operation, and to cancel it. The metadata field type is RestoreDatabaseMetadata. The response type is Database, if successful. Cancelling the returned operation will stop the restore and delete the database. There can be only one database being restored into an instance at a time. Once the restore operation completes, a new restore operation can be initiated, without waiting for the optimize operation associated with the first restore to complete.

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
databasesIdstring
instancesIdstring
projectsIdstring
pageSizeinteger (int32)
pageTokenstring
updateMaskstring (google-fieldmask)

SELECT examples

Gets the state of a Cloud Spanner database.

SELECT
name,
createTime,
databaseDialect,
defaultLeader,
earliestVersionTime,
enableDropProtection,
encryptionConfig,
encryptionInfo,
quorumInfo,
reconciling,
restoreInfo,
state,
versionRetentionPeriod
FROM google.spanner.databases
WHERE projectsId = '{{ projectsId }}' -- required
AND instancesId = '{{ instancesId }}' -- required
AND databasesId = '{{ databasesId }}' -- required;

INSERT examples

Creates a new Spanner database and starts to prepare it for serving. The returned long-running operation will have a name of the format /operations/ and can be used to track preparation of the database. The metadata field type is CreateDatabaseMetadata. The response field type is Database, if successful.

INSERT INTO google.spanner.databases (
data__createStatement,
data__extraStatements,
data__encryptionConfig,
data__databaseDialect,
data__protoDescriptors,
projectsId,
instancesId
)
SELECT
'{{ createStatement }}',
'{{ extraStatements }}',
'{{ encryptionConfig }}',
'{{ databaseDialect }}',
'{{ protoDescriptors }}',
'{{ projectsId }}',
'{{ instancesId }}'
RETURNING
name,
done,
error,
metadata,
response
;

UPDATE examples

Updates a Cloud Spanner database. The returned long-running operation can be used to track the progress of updating the database. If the named database does not exist, returns NOT_FOUND. While the operation is pending: * The database's reconciling field is set to true. * Cancelling the operation is best-effort. If the cancellation succeeds, the operation metadata's cancel_time is set, the updates are reverted, and the operation terminates with a CANCELLED status. * New UpdateDatabase requests will return a FAILED_PRECONDITION error until the pending operation is done (returns successfully or with error). * Reading the database via the API continues to give the pre-request values. Upon completion of the returned operation: * The new values are in effect and readable via the API. * The database's reconciling field becomes false. The returned long-running operation will have a name of the format projects//instances//databases//operations/ and can be used to track the database modification. The metadata field type is UpdateDatabaseMetadata. The response field type is Database, if successful.

UPDATE google.spanner.databases
SET
data__name = '{{ name }}',
data__enableDropProtection = {{ enableDropProtection }}
WHERE
projectsId = '{{ projectsId }}' --required
AND instancesId = '{{ instancesId }}' --required
AND databasesId = '{{ databasesId }}' --required
AND updateMask = '{{ updateMask}}'
RETURNING
name,
done,
error,
metadata,
response;

DELETE examples

Drops (aka deletes) a Cloud Spanner database. Completed backups for the database will be retained according to their expire_time. Note: Cloud Spanner might continue to accept requests for a few seconds after the database has been deleted.

DELETE FROM google.spanner.databases
WHERE projectsId = '{{ projectsId }}' --required
AND instancesId = '{{ instancesId }}' --required
AND databasesId = '{{ databasesId }}' --required;

Lifecycle Methods

ChangeQuorum is strictly restricted to databases that use dual-region instance configurations. Initiates a background operation to change the quorum of a database from dual-region mode to single-region mode or vice versa. The returned long-running operation has a name of the format projects//instances//databases//operations/ and can be used to track execution of the ChangeQuorum. The metadata field type is ChangeQuorumMetadata. Authorization requires spanner.databases.changequorum permission on the resource database.

EXEC google.spanner.databases.projects_instances_databases_changequorum 
@projectsId='{{ projectsId }}' --required,
@instancesId='{{ instancesId }}' --required,
@databasesId='{{ databasesId }}' --required
@@json=
'{
"name": "{{ name }}",
"quorumType": "{{ quorumType }}",
"etag": "{{ etag }}"
}';