This page describes how to manage Spanner databases, including how to create a database, modify database options, and delete a database. This page has information for both GoogleSQL-dialect databases and PostgreSQL-dialect databases. To learn how to update a database schema, see Make schema updates.
To practice creating a database and loading it with sample data, see the Spanner database codelab.
The following examples show how to create a database in an existing instance.
For GoogleSQL-dialect databases, you can define the database schema either at the time of database creation, or after the database has been created. For PostgreSQL-dialect databases you must define the schema after creation.
Schemas are defined using the Database Definition Language, which is documented for GoogleSQL and PostgreSQL. Refer to the following links for more information about creating and updating schemas:
After you create your database, you can safeguard databases that are important to your applications and services by enabling database deletion protection. For more information, see Prevent accidental database deletion.
Google Cloud consoleIn the Google Cloud console, go to the Spanner Instances page.
Select the instance to create the database in.
Click Create database.
Enter the following values:
Click Create to create the database.
Use the gcloud spanner databases create
command.
```sh
gcloud spanner databases create DATABASE \
--instance=INSTANCE \
[--async] \
[--database-dialect=DATABASE_DIALECT] \
[--ddl=DDL] \
[--ddl-file=DDL_FILE] \
[--kms-key=KMS_KEY : --kms-keyring=KMS_KEYRING --kms-location=KMS_LOCATION --kms-project=KMS_PROJECT] \
[GCLOUD_WIDE_FLAG …]
```
The following options are required:
DATABASE
--instance
flag can be omitted.
--instance=INSTANCE
The following options are optional:
--async
--database-dialect=DATABASE_DIALECT
POSTGRESQL
, GOOGLE_STANDARD_SQL
.
--ddl=DDL
--ddl_file
is set. Not supported by PostgreSQL-dialect databases.
--ddl-file=DDL_FILE
--ddl_file
is set, --ddl
is ignored. Not supported by PostgreSQL-dialect databases.
If you're specifying a Cloud Key Management Service key to use when creating the database, include the following options:
--kms-key=KMS_KEY
This flag must be specified if any of the other arguments in this group are specified. The other arguments could be omitted if the fully qualified identifier is provided.
--kms-keyring=KMS_KEYRING
--kms-location=KMS_LOCATION
--kms-project=KMS_PROJECT
To learn how to install and use the client library for Spanner, see Spanner client libraries.
To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
GoTo learn how to install and use the client library for Spanner, see Spanner client libraries.
To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
JavaTo learn how to install and use the client library for Spanner, see Spanner client libraries.
To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Note: The old client library interface code samples for Java are archived in GitHub. Node.jsTo learn how to install and use the client library for Spanner, see Spanner client libraries.
To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Note: The old client library interface code samples for Node.js are archived in GitHub. PHPTo learn how to install and use the client library for Spanner, see Spanner client libraries.
To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Note: The old client library interface code samples for PHP are archived in GitHub. PythonTo learn how to install and use the client library for Spanner, see Spanner client libraries.
To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Note: The old client library interface code samples for Python are archived in GitHub. RubyTo learn how to install and use the client library for Spanner, see Spanner client libraries.
To authenticate to Spanner, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
You can update your database schema and options using DDL statements.
GoogleSQLALTER TABLE Songwriters ADD COLUMN Publisher STRING(10);
PostgreSQL
ALTER TABLE Songwriters ADD COLUMN Publisher VARCHAR(10);
GoogleSQL
ALTER DATABASE Music SET OPTIONS(optimizer_version=null);
PostgreSQL
ALTER DATABASE DB-NAME SET spanner.optimizer_version TO DEFAULT;
For more information about supported options, refer to the ALTER DATABASE
DDL reference for GoogleSQL or PostgreSQL.
In the Google Cloud console, go to the Spanner Instances page.
Select the instance containing the database to alter.
Select the database.
Click Spanner Studio.
Click add New tab or use the empty editor tab. Then, enter the DDL statements to apply.
Click Run to apply the updates. If there are errors in your DDL, the Google Cloud console returns an error and the database is not altered.
To alter a database with the gcloud
command-line tool, use gcloud spanner databases ddl update
.
gcloud spanner databases ddl update \ (DATABASE : --instance=INSTANCE) \ [--async] \ [--ddl=DDL] \ [--ddl-file=DDL_FILE] \
Refer to the gcloud
reference for details about the available options.
Pass the database updates to the command with either the --ddl
flag, or the --ddl-file
flag. If a DDL file is specified, the --ddl
flag is ignored.
Refer to the ALTER DATABASE
DDL reference for GoogleSQL or PostgreSQL for the DDL statements to include.
Refer to the ALTER DATABASE
DDL reference for GoogleSQL or PostgreSQL for details.
In the Spanner navigation menu, select the Operations tab. The Operations page shows a list of active running operations.
Find the schema operation in the list. If it's still running, the progress bar in the End time column shows the percentage of the operation that is complete, as shown in the following image:
Use gcloud spanner operations describe
to check the progress of an operation.
Get the operation ID:
gcloud spanner operations list --instance=INSTANCE-NAME \ --database=DATABASE-NAME --type=DATABASE_UPDATE_DDL
Replace the following:
Run gcloud spanner operations describe
:
gcloud spanner operations describe OPERATION_ID\ --instance=INSTANCE-NAME \ --database=DATABASE-NAME
Replace the following:
The progress
section in the output shows the percentage of the operation that's complete. The output looks similar to the following:
done: true metadata: ... progress: - endTime: '2022-03-01T00:28:06.691403Z' progressPercent: 100 startTime: '2022-03-01T00:28:04.221401Z' - endTime: '2022-03-01T00:28:17.624588Z' startTime: '2022-03-01T00:28:06.691403Z' progressPercent: 100 ...
Get the operation ID:
gcloud spanner operations list --instance=INSTANCE-NAME \ --database=DATABASE-NAME --type=DATABASE_UPDATE_DDL
Replace the following:
Check the progress for the operation.
Before using any of the request data, make the following replacements:
HTTP method and URL:
GET https://spanner.googleapis.com/v1/projects/PROJECT-ID/instances/INSTANCE-ID/databases/DATABASE-ID/operations/OPERATION-ID
To send your request, expand one of these options:
curl (Linux, macOS, or Cloud Shell) Note: The following command assumes that you have logged in to thegcloud
CLI with your user account by running gcloud init
or gcloud auth login
, or by using Cloud Shell, which automatically logs you into the gcloud
CLI . You can check the currently active account by running gcloud auth list
.
Execute the following command:
curl -X GET \PowerShell (Windows) Note: The following command assumes that you have logged in to the
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://spanner.googleapis.com/v1/projects/PROJECT-ID/instances/INSTANCE-ID/databases/DATABASE-ID/operations/OPERATION-ID"
gcloud
CLI with your user account by running gcloud init
or gcloud auth login
. You can check the currently active account by running gcloud auth list
.
Execute the following command:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://spanner.googleapis.com/v1/projects/PROJECT-ID/instances/INSTANCE-ID/databases/DATABASE-ID/operations/OPERATION-ID" | Select-Object -Expand Content
You should receive a JSON response similar to the following:
{ ... "progress": [ { "progressPercent": 100, "startTime": "2023-05-27T00:52:27.366688Z", "endTime": "2023-05-27T00:52:30.184845Z" }, { "progressPercent": 100, "startTime": "2023-05-27T00:52:30.184845Z", "endTime": "2023-05-27T00:52:40.750959Z" } ], ... "done": true, "response": { "@type": "type.googleapis.com/google.protobuf.Empty" } }
Deleting a database permanently removes the database and all its data. Database deletion can't be undone. If database deletion protection is enabled on a database, you can't delete that database until you disable its deletion protection.
Existing backups are not deleted when a database is deleted. For more information, see Backup and restore.
Google Cloud consoleIn the Google Cloud console, go to the Spanner Instances page.
Select the instance containing the database to delete.
Select the database.
Click Delete database. A confirmation appears.
Type the database name and click Delete.
To delete a database with the gcloud
command-line tool, use gcloud spanner databases delete
.
gcloud spanner databases delete \ (DATABASE : --instance=INSTANCE)
The following options are required:
DATABASE
--instance
flag should be omitted.
--instance=INSTANCE
For more details refer to the gcloud
reference.
DDL does not support database deletion syntax.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4