Connect to Cloud SQL for PostgreSQL from Google Kubernetes Engine
This page shows you how to deploy a sample app on Google Kubernetes Engine (GKE) connected to a PostgreSQL instance using the Google Cloud console and a client application. The resources created in this quickstart typically cost less than one dollar (USD), assuming you complete the steps, including the clean up, in a timely manner.
Before you begin
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that you have the permissions required to complete this guide.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Artifact Registry, Cloud Build, Cloud SQL Admin, Compute Engine, Kubernetes Engine, Service Networking APIs.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
Install the gcloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init -
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that you have the permissions required to complete this guide.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Artifact Registry, Cloud Build, Cloud SQL Admin, Compute Engine, Kubernetes Engine, Service Networking APIs.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
Install the gcloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
Required roles
To get the permissions that you need to complete the tasks described in this quickstart, ask your administrator to grant you the following IAM roles on your quickstart project:
-
Create, update, or delete all Cloud SQL instances, databases, and users:
- Cloud SQL Admin (
roles/cloudsql.admin) - Compute Viewer (
roles/compute.viewer)
- Cloud SQL Admin (
-
Create a Cloud SQL instance with private IP (private services access connection):
Compute Network Admin (
roles/compute.networkadmin) -
Create and manage a Google Kubernetes Engine cluster:
Kubernetes Engine Admin (
roles/container.admin) -
Create and manage service accounts for the project:
- Project IAM Admin (
roles/resourcemanager.projectIamAdmin) - Service Account Admin (
roles/serviceAccountAdmin)
- Project IAM Admin (
-
Create a repository in Artifact Registry for the project:
Artifact Registry Administrator (
roles/artifactregistry.admin) -
Submit and build applications in Cloud Build:
- Cloud Build Editor (
roles/cloudbuilds.builds.editor) - Storage Object Creator (
roles/storage.objectCreator) - Storage Bucket Viewer (
roles/storage.bucketViewer) - Service Account User (
roles/iam.serviceAccountUser) - Logs Viewer (
roles/logging.viewer)
- Cloud Build Editor (
For more information about granting roles, see Manage access to projects, folders, and organizations.
You might also be able to get the required permissions through custom roles or other predefined roles.
Set up Cloud SQL
Create a Cloud SQL instance
Create a database
Take the following steps to create a database:
Console
-
In the Google Cloud console, go to the Cloud SQL Instances page.
- Select
quickstart-instance. - From the SQL navigation menu, select Databases.