建立及管理資料庫
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
本頁面提供在 Cloud SQL 執行個體上建立、列出及刪除 PostgreSQL 資料庫的相關資訊。
新建立的執行個體含有 postgres
資料庫。
如要進一步瞭解 PostgreSQL 資料庫,請參閱
PostgreSQL 說明文件。
事前準備
完成此頁面上的工作之前,您必須已經:
- 建立 Cloud SQL 執行個體。詳情請參閱「建立執行個體」。
如果您想要使用 psql 用戶端建立或管理資料庫,您必須已經:
- 設定存取權,並透過
psql
用戶端連線至執行個體。如要進一步瞭解連線選項,請參閱「外部應用程式的連線選項」。
在 Cloud SQL 執行個體上建立資料庫
控制台
-
前往 Google Cloud 控制台的「Cloud SQL 執行個體」頁面。
前往 Cloud SQL 執行個體
-
如要開啟執行個體的「總覽」頁面,請按一下執行個體名稱。
- 在 SQL 導覽選單中選取「資料庫」。
- 按一下 [Create database] (建立資料庫)。
- 在「New database」對話方塊中,指定資料庫名稱。
- 按一下 [建立]。
gcloud
如需參考資訊,請參閱 gcloud
sql databases create
。
gcloud sql databases create DATABASE_NAME \
--instance=INSTANCE_NAME
如要建立資料庫,請使用 Terraform 資源。
套用變更
如要在 Google Cloud 專案中套用 Terraform 設定,請完成下列各節中的步驟。
準備 Cloud Shell
- 啟動 Cloud Shell。
-
設定要套用 Terraform 設定的預設 Google Cloud 專案。
您只需為每個專案執行這個指令一次,而且可以在任何目錄中執行。
export GOOGLE_CLOUD_PROJECT=PROJECT_ID
如果您在 Terraform 設定檔中設定明確的值,系統就會覆寫環境變數。
準備目錄
每個 Terraform 設定檔都必須有自己的目錄 (也稱為根模組)。
-
在 Cloud Shell 中建立目錄,並在該目錄中建立新檔案。檔案名稱必須包含
.tf
副檔名,例如 main.tf
。在本教學課程中,檔案稱為 main.tf
。mkdir DIRECTORY && cd DIRECTORY && touch main.tf
-
如果您正在參考教學課程,可以複製各個章節或步驟中的程式碼範例。
將範例程式碼複製到新建立的 main.tf
中。
您可以視需要從 GitHub 複製程式碼。如果 Terraform 程式碼片段是端對端解決方案的一部分,建議您採用這種做法。
- 查看並修改要套用至環境的範例參數。
- 儲存變更。
-
初始化 Terraform。這項操作只需對每個目錄執行一次。
terraform init
如要使用最新版的 Google 供應器,請視需要加入 -upgrade
選項:
terraform init -upgrade
套用變更
-
檢查設定,並確認 Terraform 要建立或更新的資源是否符合您的預期:
terraform plan
視需要修正設定。
-
執行下列指令,並在提示中輸入
yes
,即可套用 Terraform 設定:terraform apply
等待 Terraform 顯示「Apply complete!」(套用完成) 訊息。
- 開啟 Google Cloud 專案即可查看結果。在 Google Cloud 控制台中,前往 UI 中的資源,確認 Terraform 已建立或更新這些資源。
刪除變更
如要刪除變更,請按照下列步驟操作:
- 如要停用防刪除功能,請在 Terraform 設定檔中將
deletion_protection
引數設為 false
。deletion_protection = "false"
- 執行下列指令,並在提示中輸入
yes
,即可套用更新的 Terraform 設定:terraform apply
-
如要移除先前透過 Terraform 設定套用的資源,請執行下列指令,並在提示中輸入 yes
:
terraform destroy
REST v1
以下要求使用 databases:insert 方法在指定執行個體上建立新資料庫。
使用任何要求資料之前,請先替換以下項目:
- project-id:專案 ID
- instance-id:執行個體 ID
- database-name:Cloud SQL 執行個體內的資料庫名稱
HTTP 方法和網址:
POST https://sqladmin.googleapis.com/v1/projects/project-id/instances/instance-id/databases
JSON 要求主體:
{
"project": "project-id",
"instance": "instance-id",
"name": "database-name"
}
如要傳送要求,請展開以下其中一個選項:
curl (Linux、macOS 或 Cloud Shell)
將要求主體儲存在名為 request.json
的檔案中,然後執行下列指令:
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)
將要求主體儲存在名為 request.json
的檔案中,然後執行下列指令:
$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
您應該會收到如下的 JSON 回應:
回應
{
"kind": "sql#operation",
"targetLink": "https://sqladmin.googleapis.com/v1/projects/project-id/instances/instance-id",
"status": "PENDING",
"user": "[email protected]",
"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
以下要求使用 databases:insert 方法在指定執行個體上建立新資料庫。
使用任何要求資料之前,請先替換以下項目:
- project-id:專案 ID
- instance-id:執行個體 ID
- database-name:Cloud SQL 執行個體內的資料庫名稱
HTTP 方法和網址:
POST https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id/databases
JSON 要求主體:
{
"project": "project-id",
"instance": "instance-id",
"name": "database-name"
}
如要傳送要求,請展開以下其中一個選項:
curl (Linux、macOS 或 Cloud Shell)
將要求主體儲存在名為 request.json
的檔案中,然後執行下列指令:
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)
將要求主體儲存在名為 request.json
的檔案中,然後執行下列指令:
$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
您應該會收到如下的 JSON 回應:
回應
{
"kind": "sql#operation",
"targetLink": "https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id",
"status": "PENDING",
"user": "[email protected]",
"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"
}
psql 用戶端
如需參考資訊,請參閱 PostgreSQL 說明文件的「CREATE DATABASE」一節。
請參閱 PostgreSQL 說明文件中的下列主題,瞭解相關背景和支援的字元集值清單。
CREATE DATABASE database_name
[[ ENCODING encoding ][ LC_COLLATE lc_collate ]];
例如:
CREATE DATABASE "example_db" WITH OWNER "example_user"
ENCODING 'UTF8' LC_COLLATE = 'pl_PL.utf8' LC_CTYPE = 'pl_PL.utf8' TEMPLATE template0;
列出資料庫
如何列出執行個體上的所有資料庫:
控制台
-
前往 Google Cloud 控制台的「Cloud SQL 執行個體」頁面。
前往 Cloud SQL 執行個體
-
如要開啟執行個體的「總覽」頁面,請按一下執行個體名稱。
- 按一下左側選單中的「資料庫」。「資料庫」頁面會列出資料庫及其排序類型、字元集和資料庫類型。
REST v1
以下要求使用 databases:list 方法列出執行個體的資料庫。
當您使用 API 列出資料庫時,會看見主控台未顯示的其他範本資料庫與系統資料庫。請注意,您無法刪除或管理系統資料庫。
使用任何要求資料之前,請先替換以下項目:
- project-id:專案 ID
- instance-id:執行個體 ID
HTTP 方法和網址:
GET https://sqladmin.googleapis.com/v1/projects/project-id/instances/instance-id/databases
如要傳送要求,請展開以下其中一個選項:
curl (Linux、macOS 或 Cloud Shell)
執行下列指令:
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)
執行下列指令:
$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
您應該會收到如下的 JSON 回應:
回應
{
"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"
}
REST v1beta4
以下要求使用 databases:list 方法列出執行個體的資料庫。
當您使用 API 列出資料庫時,會看見主控台未顯示的其他範本資料庫與系統資料庫。請注意,您無法刪除或管理系統資料庫。
使用任何要求資料之前,請先替換以下項目:
- project-id:專案 ID
- instance-id:執行個體 ID
HTTP 方法和網址:
GET https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id/databases
如要傳送要求,請展開以下其中一個選項:
curl (Linux、macOS 或 Cloud Shell)
執行下列指令:
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)
執行下列指令:
$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
您應該會收到如下的 JSON 回應:
回應
{
"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"
}
psql 用戶端
如需參考資訊,請參閱「list」一節。
當您使用 psql
用戶端列出資料庫時,會看見主控台未顯示的其他範本資料庫與系統資料庫。請注意,您無法刪除或管理系統資料庫。
\l
刪除資料庫
如何刪除在 Cloud SQL 執行個體上的資料庫:
控制台
-
前往 Google Cloud 控制台的「Cloud SQL 執行個體」頁面。
前往 Cloud SQL 執行個體
-
如要開啟執行個體的「總覽」頁面,請按一下執行個體名稱。
- 按一下左側選單中的「資料庫」。
- 在資料庫清單中,找到您要刪除的資料庫並按一下垃圾桶圖示。
- 在「Delete database」對話方塊中輸入資料庫名稱,然後按一下「Delete」。
REST v1
以下要求使用 databases:delete 方法刪除指定資料庫。
使用任何要求資料之前,請先替換以下項目:
- project-id:專案 ID
- instance-id:執行個體 ID
- database-name:Cloud SQL 執行個體內的資料庫名稱
HTTP 方法和網址:
DELETE https://sqladmin.googleapis.com/v1/projects/project-id/instances/instance-id/databases/database-name
如要傳送要求,請展開以下其中一個選項:
curl (Linux、macOS 或 Cloud Shell)
執行下列指令:
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)
執行下列指令:
$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
您應該會收到如下的 JSON 回應:
回應
{
"kind": "sql#operation",
"targetLink": "https://sqladmin.googleapis.com/v1/projects/project-id/instances/instance-id",
"status": "PENDING",
"user": "[email protected]",
"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
以下要求使用 databases:delete 方法刪除指定資料庫。
使用任何要求資料之前,請先替換以下項目:
- project-id:專案 ID
- instance-id:執行個體 ID
- database-name:Cloud SQL 執行個體內的資料庫名稱
HTTP 方法和網址:
DELETE https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id/databases/database-name
如要傳送要求,請展開以下其中一個選項:
curl (Linux、macOS 或 Cloud Shell)
執行下列指令:
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)
執行下列指令:
$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
您應該會收到如下的 JSON 回應:
回應
{
"kind": "sql#operation",
"targetLink": "https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id",
"status": "PENDING",
"user": "[email protected]",
"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"
}
psql 用戶端
如需參考資訊,請參閱 PostgreSQL 說明文件中的「DROP DATABASE」一節。
DROP DATABASE [database_name];
後續步驟
歡迎試用
如果您未曾使用過 Google Cloud,歡迎建立帳戶,親自體驗實際使用 Cloud SQL 的成效。新客戶可以獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
免費試用 Cloud SQL
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-05-16 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-05-16 (世界標準時間)。"],[],[]]