Create and manage databases Stay organized with collections Save and categorize content based on your preferences.
This page contains information about creating, listing, and deleting SQL Serverdatabases on a Cloud SQL instance.
A newly-created instance has asqlserver database.
For information about setting a default value for the type ofcollationused for the databases in an instance, seeCreating instances.
Before you begin
Before completing the tasks on this page, you must have:
- Created a Cloud SQL instance. For more information,seeCreating instances.
If you plan to use the sqlcmd client to create or manage your databases,you must have:
- Configured access and connected to the instance with a
sqlcmdclient.
Create a database on the Cloud SQL instance
Note: The maximum length of the database name is 128 characters.For more information on other criteria for this name, seeDatabase Identifiers.Console
In the Google Cloud console, go to theCloud SQL Instances page.
- To open theOverview page of an instance, click the instance name.
- SelectDatabases from the SQL navigation menu.
- ClickCreate database.
- In theNew database dialog, specify the name of the database.
- ClickCreate.
gcloud
For reference information, seegcloud sql databases create.
For information about collations in SQL Server, seeCollation and Unicode Support in the SQL Server documentation.
gcloudsqldatabasescreateDATABASE_NAME\--instance=INSTANCE_NAME\[--charset=CHARSET]\[--collation=COLLATION]
Terraform
To create a database, use aTerraform resource.
resource "google_sql_database" "database" { name = "my-database" instance = google_sql_database_instance.instance.name}Apply the changes
To apply your Terraform configuration in a Google Cloud project, complete the steps in the following sections.
Prepare Cloud Shell
- LaunchCloud Shell.
Set the default Google Cloud project where you want to apply your Terraform configurations.
You only need to run this command once per project, and you can run it in any directory.
export GOOGLE_CLOUD_PROJECT=PROJECT_ID
Environment variables are overridden if you set explicit values in the Terraform configuration file.
Prepare the directory
Each Terraform configuration file must have its own directory (alsocalled aroot module).
- InCloud Shell, create a directory and a new file within that directory. The filename must have the
.tfextension—for examplemain.tf. In this tutorial, the file is referred to asmain.tf.mkdirDIRECTORY && cdDIRECTORY && touch main.tf
If you are following a tutorial, you can copy the sample code in each section or step.
Copy the sample code into the newly created
main.tf.Optionally, copy the code from GitHub. This is recommended when the Terraform snippet is part of an end-to-end solution.
- Review and modify the sample parameters to apply to your environment.
- Save your changes.
- Initialize Terraform. You only need to do this once per directory.
terraform init
Optionally, to use the latest Google provider version, include the
-upgradeoption:terraform init -upgrade
Apply the changes
- Review the configuration and verify that the resources that Terraform is going to create or update match your expectations:
terraform plan
Make corrections to the configuration as necessary.
- Apply the Terraform configuration by running the following command and entering
yesat the prompt:terraform apply
Wait until Terraform displays the "Apply complete!" message.
- Open your Google Cloud project to view the results. In the Google Cloud console, navigate to your resources in the UI to make sure that Terraform has created or updated them.
Delete the changes
To delete your changes, do the following:
- To disable deletion protection, in your Terraform configuration file set the
deletion_protectionargument tofalse.deletion_protection = "false"
- Apply the updated Terraform configuration by running the following command and entering
yesat the prompt:terraform apply
Remove resources previously applied with your Terraform configuration by running the following command and entering
yesat the prompt:terraform destroy
REST v1
The following request uses thedatabases:insert method to create a new database on the specified instance.
Before using any of the request data, make the following replacements:
- project-id: The project ID
- instance-id: The instance ID
- database-name: The name of a database inside the Cloud SQL instance
HTTP method and URL:
POST https://sqladmin.googleapis.com/v1/projects/project-id/instances/instance-id/databases
Request JSON body:
{ "project": "project-id", "instance": "instance-id", "name": "database-name"}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 runninggcloud init orgcloud auth login , or by usingCloud Shell, which automatically logs you into thegcloud CLI . You can check the currently active account by runninggcloud auth list. Save the request body in a file namedrequest.json, and execute the following command:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://sqladmin.googleapis.com/v1/projects/project-id/instances/instance-id/databases"
PowerShell (Windows)
Note: The following command assumes that you have logged in to thegcloud CLI with your user account by runninggcloud init orgcloud auth login . You can check the currently active account by runninggcloud auth list. Save the request body in a file namedrequest.json, and execute the following command:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://sqladmin.googleapis.com/v1/projects/project-id/instances/instance-id/databases" | Select-Object -Expand Content
You should receive a JSON response similar to the following:
Response
{ "kind": "sql#operation", "targetLink": "https://sqladmin.googleapis.com/v1/projects/project-id/instances/instance-id", "status": "PENDING", "user": "user@example.com", "insertTime": "2020-01-21T22:43:37.981Z", "operationType": "CREATE_DATABASE", "name": "operation-id", "targetId": "instance-id", "selfLink": "https://sqladmin.googleapis.com/v1/projects/project-id/operations/operation-id", "targetProject": "project-id"}REST v1beta4
The following request uses thedatabases:insert method to create a new database on the specified instance.
Before using any of the request data, make the following replacements:
- project-id: The project ID
- instance-id: The instance ID
- database-name: The name of a database inside the Cloud SQL instance
HTTP method and URL:
POST https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id/databases
Request JSON body:
{ "project": "project-id", "instance": "instance-id", "name": "database-name"}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 runninggcloud init orgcloud auth login , or by usingCloud Shell, which automatically logs you into thegcloud CLI . You can check the currently active account by runninggcloud auth list. Save the request body in a file namedrequest.json, and execute the following command:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id/databases"
PowerShell (Windows)
Note: The following command assumes that you have logged in to thegcloud CLI with your user account by runninggcloud init orgcloud auth login . You can check the currently active account by runninggcloud auth list. Save the request body in a file namedrequest.json, and execute the following command:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id/databases" | Select-Object -Expand Content
You should receive a JSON response similar to the following:
Response
{ "kind": "sql#operation", "targetLink": "https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id", "status": "PENDING", "user": "user@example.com", "insertTime": "2020-01-21T22:43:37.981Z", "operationType": "CREATE_DATABASE", "name": "operation-id", "targetId": "instance-id", "selfLink": "https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/operations/operation-id", "targetProject": "project-id"}List your databases
To list all databases on an instance:
Console
In the Google Cloud console, go to theCloud SQL Instances page.
- To open theOverview page of an instance, click the instance name.
- ClickDatabases from the left side menu. TheDatabases page lists databases along with their collation type, character set, and database type.
gcloud
For reference information, seegcloud sql databases list.
gcloudsqldatabaseslist\--instance=INSTANCE_NAME
REST v1
The following request uses thedatabases:list method to list the databases for an instance.
When you list the databases using the API, you seeadditional template databases and a system database that are not displayed bythe console. You cannot delete or manage the system database.
Before using any of the request data, make the following replacements:
- project-id: The project ID
- instance-id: The instance ID
HTTP method and URL:
GET https://sqladmin.googleapis.com/v1/projects/project-id/instances/instance-id/databases
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 runninggcloud init orgcloud auth login , or by usingCloud Shell, which automatically logs you into thegcloud CLI . You can check the currently active account by runninggcloud auth list.Execute the following command:
curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://sqladmin.googleapis.com/v1/projects/project-id/instances/instance-id/databases"
PowerShell (Windows)
Note: The following command assumes that you have logged in to thegcloud CLI with your user account by runninggcloud init orgcloud auth login . You can check the currently active account by runninggcloud auth list.Execute the following command:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://sqladmin.googleapis.com/v1/projects/project-id/instances/instance-id/databases" | Select-Object -Expand Content
You should receive a JSON response similar to the following:
Response
{ "kind": "sql#database", "charset": "utf8", "collation": "utf8_general_ci", "etag": "etag", "name": "sys", "instance": "instance-id", "selfLink": "https://sqladmin.googleapis.com/v1/projects/project-id/instances/instance-id/databases/sys", "project": "project-id"}Note: Thedatabases.list API supports only response payloads thatinclude up to 4 MB of text.
If you run a database list request on alarge instance, then you might receive an error. If this occurs, then to get thelist of databases, we recommend that you run the request directly from theinstance.
REST v1beta4
The following request uses thedatabases:list method to list the databases for an instance.
When you list the databases using the API, you seeadditional template databases and a system database that are not displayed bythe console. You cannot delete or manage the system database.
Before using any of the request data, make the following replacements:
- project-id: The project ID
- instance-id: The instance ID
HTTP method and URL:
GET https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id/databases
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 runninggcloud init orgcloud auth login , or by usingCloud Shell, which automatically logs you into thegcloud CLI . You can check the currently active account by runninggcloud auth list.Execute the following command:
curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id/databases"
PowerShell (Windows)
Note: The following command assumes that you have logged in to thegcloud CLI with your user account by runninggcloud init orgcloud auth login . You can check the currently active account by runninggcloud auth list.Execute the following command:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id/databases" | Select-Object -Expand Content
You should receive a JSON response similar to the following:
Response
{ "kind": "sql#database", "charset": "utf8", "collation": "utf8_general_ci", "etag": "etag", "name": "sys", "instance": "instance-id", "selfLink": "https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id/databases/sys", "project": "project-id"}Note: Thedatabases.list API supports only response payloads thatinclude up to 4 MB of text.
If you run a database list request on alarge instance, then you might receive an error. If this occurs, then to get thelist of databases, we recommend that you run the request directly from theinstance.
Change a database owner
If you want to drop the user login associated with a database owner that's nolonger active, you must first assign a new database owner.
To add a new database owner, complete the following:
gcloud
- Connect to your database and create two demo accounts:
createloginTEST_LOGINwithpassword='TEST_PASSWORD'createloginREPORT_USERwithpassword='REPORT_USER_PASSWORD'GRANTCREATEANYDATABASETOTEST_LOGINAS[CustomerDbRootRole]
Make the following replacements:
- TEST_LOGIN: the name of the test account, such as
test-login. - TEST_PASSWORD: the password for the test account.
- REPORT_USER: the name of a second user account, such as
report-user. This account becomes the new database owner. - REPORT_USER_PASSWORD: the password for the second user account.
- TEST_LOGIN: the name of the test account, such as
- Connect to your database using the test account you just created. Then, create a test database using the following command:
CREATEDATABASETEST_DB
Make the following replacements:
- TEST_DB: the name of the test database you want to create, such as
test-database - TEST_LOGIN: The name of the test account, such as
test-login.
The database owner is automatically set to the test account.
- TEST_DB: the name of the test database you want to create, such as
- Connect to the database using the SQL Server login. Then, run the following command to try to drop the login
TEST_LOGIN:droploginTEST_LOGIN
ReplaceTEST_LOGIN with the name of the test account, such as
test-login.The operation fails and the following error is returned:
Login
TEST_LOGINowns one or more database(s). Change the owner of the database(s) before dropping the login. - Connect to the
TEST_DBdatabase as theREPORT_USERaccount and grant the account permissions to match the existing owner.GRANTIMPERSONATEonLOGIN::REPORT_USERtoTEST_LOGIN
- Connect to the database as the test account. Then, run the following command to authorize the second account as a database owner:
ALTERAUTHORIZATIONONDATABASE::TEST_DBTOREPORT_USER
- Connect to the database as the
REPORT_USERand remove any permissions granted.REVOKEIMPERSONATEonLOGIN::REPORT_USERtoTEST_LOGIN
- Connect to the database as the SQL Server account. Then, run the following command to drop the
TEST_LOGINlogin: When finished, the following message is returned:DROPLOGINTEST_LOGIN
DROP LOGIN forTEST_LOGIN by sqlserver(sqlserver) is successful.
Delete a database
To delete a database on the Cloud SQL instance:
Console
In the Google Cloud console, go to theCloud SQL Instances page.
- To open theOverview page of an instance, click the instance name.
- ClickDatabases from the left side menu.
- In the database list, find the database you want to delete and click the trash can icon.
- In theDelete database dialog, enter the name of the database and then clickDelete.
gcloud
For reference information, seegcloud sql databases delete.
gcloudsqldatabasesdeleteDATABASE_NAME\--instance=INSTANCE_NAME
REST v1
The following request uses thedatabases:delete method to delete the specified database.
Before using any of the request data, make the following replacements:
- project-id: The project ID
- instance-id: The instance ID
- database-name: The name of a database inside the Cloud SQL instance
HTTP method and URL:
DELETE https://sqladmin.googleapis.com/v1/projects/project-id/instances/instance-id/databases/database-name
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 runninggcloud init orgcloud auth login , or by usingCloud Shell, which automatically logs you into thegcloud CLI . You can check the currently active account by runninggcloud auth list.Execute the following command:
curl -X DELETE \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://sqladmin.googleapis.com/v1/projects/project-id/instances/instance-id/databases/database-name"
PowerShell (Windows)
Note: The following command assumes that you have logged in to thegcloud CLI with your user account by runninggcloud init orgcloud auth login . You can check the currently active account by runninggcloud auth list.Execute the following command:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method DELETE `
-Headers $headers `
-Uri "https://sqladmin.googleapis.com/v1/projects/project-id/instances/instance-id/databases/database-name" | Select-Object -Expand Content
You should receive a JSON response similar to the following:
Response
{ "kind": "sql#operation", "targetLink": "https://sqladmin.googleapis.com/v1/projects/project-id/instances/instance-id", "status": "PENDING", "user": "user@example.com", "insertTime": "2020-01-21T22:43:37.981Z", "operationType": "DELETE_DATABASE", "name": "operation-id", "targetId": "instance-id", "selfLink": "https://sqladmin.googleapis.com/v1/projects/project-id/operations/operation-id", "targetProject": "project-id"}REST v1beta4
The following request uses thedatabases:delete method to delete the specified database.
Before using any of the request data, make the following replacements:
- project-id: The project ID
- instance-id: The instance ID
- database-name: The name of a database inside the Cloud SQL instance
HTTP method and URL:
DELETE https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id/databases/database-name
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 runninggcloud init orgcloud auth login , or by usingCloud Shell, which automatically logs you into thegcloud CLI . You can check the currently active account by runninggcloud auth list.Execute the following command:
curl -X DELETE \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id/databases/database-name"
PowerShell (Windows)
Note: The following command assumes that you have logged in to thegcloud CLI with your user account by runninggcloud init orgcloud auth login . You can check the currently active account by runninggcloud auth list.Execute the following command:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method DELETE `
-Headers $headers `
-Uri "https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id/databases/database-name" | Select-Object -Expand Content
You should receive a JSON response similar to the following:
Response
{ "kind": "sql#operation", "targetLink": "https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id", "status": "PENDING", "user": "user@example.com", "insertTime": "2020-01-21T22:43:37.981Z", "operationType": "DELETE_DATABASE", "name": "operation-id", "targetId": "instance-id", "selfLink": "https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/operations/operation-id", "targetProject": "project-id"}Troubleshooting
If a database haschange data capture (CDC)enabled, and you try to delete it from the instance, then the operationfails. You might also receive an unknown error message. If this occurs, completethe following steps:
- Use the
msdb.dbo.gcloudsql_cdc_disable_dbstored procedure todisable CDC for the database. - Check if alinked server is configured for the database. If one exists, drop it.
- Run the delete operation again.
For more information, seeDelete a CDC database.
What's next
- Manage users for your instance.
- Import data into your instance.
- Create Tables (Database Engine).
Try it for yourself
If you're new to Google Cloud, create an account to evaluate how Cloud SQL performs in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
Try Cloud SQL freeExcept as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2026-02-19 UTC.