A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cloud.google.com/sql/docs/postgres/connect-instance-cloud-run below:

Quickstart: Connect to Cloud SQL for PostgreSQL from Cloud Run

Connect to Cloud SQL for PostgreSQL from Cloud Run

Learn how to deploy a sample app on Cloud Run connected to a PostgreSQL instance by using the Google Cloud console and a client application.

Assuming that you complete all the steps in a timely manner, the resources created in this quickstart typically cost less than one dollar (USD).

To follow step-by-step guidance for this task directly in the Google Cloud console, click Guide me:

Guide me

Before you begin Note: The name you use for your project must be between 4 and 30 characters. When you type the name, the form suggests a project ID, which you can edit. The project ID must be between 6 and 30 characters, with a lowercase letter as the first character. You can use a dash, lowercase letter, or digit for the remaining characters, but the last character cannot be a dash.
  1. 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.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.

    Go to project selector

  5. Make sure that billing is enabled for your Google Cloud project.

  6. Enable the Cloud APIs necessary to run a Cloud SQL sample app on Cloud Run. Console

    Click Enable APIs to enable the APIs required for this quickstart.

    Enable APIs

    This enables the following APIs:

    gcloud

    Click the following button to open Cloud Shell, which provides command-line access to your Google Cloud resources directly from the browser. Cloud Shell can be used to run the gcloud commands presented throughout this quickstart.

    Open Cloud Shell

    Run the following gcloud command using Cloud Shell:

    gcloud services enable compute.googleapis.com sqladmin.googleapis.com run.googleapis.com \
    containerregistry.googleapis.com cloudbuild.googleapis.com servicenetworking.googleapis.com

    This command enables the following APIs:

Set up Cloud SQL Create a Cloud SQL instance Public IP Console
  1. In the Google Cloud console, go to the Cloud SQL Instances page.

    Go to Cloud SQL Instances

  2. Click Create instance.
  3. Click Choose PostgreSQL.
  4. Make sure that Enterprise Plus is selected as the Cloud SQL edition for your instance.
  5. In the Instance ID field, enter quickstart-instance.
  6. In the Password field, enter a password for the postgres user. Save this password for future use.
  7. In the Choose region and zonal availability section, select Single zone.
  8. Click the Show configuration options menu.
  9. Expand the Machine configuration node.
  10. From the Machine shapes region, select the 4 vCPU, 32 GB shape.
  11. Click Create instance and then wait until the instance initializes and starts.
gcloud

Before running the command as follows, replace DB_ROOT_PASSWORD with the password of your database user.

Optionally, modify the values for the following parameters:

Run the gcloud sql instances create command to create a Cloud SQL instance.

gcloud sql instances create quickstart-instance \
--database-version=POSTGRES_14 \
 --cpu=1 \
 --memory=4GB \
 --region=us-central \
 --root-password=DB_ROOT_PASSWORD
Private IP Console Allocate an IP address range and create a private connection to configure private services access for Cloud SQL
  1. In the Google Cloud console, go to the VPC networks page.

    Go to VPC networks

  2. Select the default VPC network.
  3. Select the Private service connection tab.
  4. Select the Allocated IP ranges for services tab.
  5. Click Allocate IP range.
  6. For the Name of the allocated range, specify google-managed-services-default.
  7. Select the Automatic option for IP range and specify the prefix length as 16.
  8. Click Allocate to create the allocated range.
  9. Select the Private connections to services tab for the default VPC network.
  10. Click Create connection to create a private connection between your network and a service producer.
  11. For the Assigned allocation, select google-managed-services-default.
  12. Click Connect to create the connection.
Create an instance with private IP address and SSL enabled
  1. In the Google Cloud console, go to the Cloud SQL Instances page.

    Go to Cloud SQL Instances

  2. Click Create instance.
  3. Click Choose PostgreSQL.
  4. Make sure that Enterprise Plus is selected as the Cloud SQL edition for your instance.
  5. In the Instance ID field, enter quickstart-instance.
  6. In the Password field, enter a password for the postgres user. Save this password for future use.
  7. In the Choose region and zonal availability section, select Single zone.
  8. Click the Show configuration options menu.
  9. Expand the Machine configuration node.
  10. From the Machine shapes region, select the 4 vCPU, 32 GB shape.
  11. Expand the Connections node.
  12. Clear the Public IP checkbox to create an instance only with a private IP address.
  13. Select the Private IP checkbox.
  14. From the Network menu, select default.
  15. Click Create instance and then wait for the instance to initialize and start.
  16. Click Connections.
  17. In the Security section, select Allow only SSL connections to enable SSL connections.
  18. In the Allow only SSL connections dialog, click Save and then wait for the instance to restart.
gcloud Allocate an IP address range and create a private connection to configure private services access for Cloud SQL
  1. Run the gcloud compute addresses create command to allocate an IP address range.

    gcloud compute addresses create google-managed-services-default \
    --global --purpose=VPC_PEERING --prefix-length=16 \
    --description="peering range for Google" --network=default
  2. Run the gcloud services vpc-peerings connect command to create a private connection to the allocated IP address range. Replace YOUR_PROJECT_ID with your project's project ID.

    gcloud services vpc-peerings connect --service=servicenetworking.googleapis.com \
    --ranges=google-managed-services-default --network=default \
    --project=YOUR_PROJECT_ID
Create an instance with private IP address and SSL enabled
  1. Before running the command as follows, replace DB_ROOT_PASSWORD with the password of your database user.

  2. Optionally, modify the values for the following parameters:

    Run the gcloud sql instances create command to create a Cloud SQL instance with a Private IP address.

    gcloud sql instances create quickstart-instance \
    --database-version=POSTGRES_14 \
     --cpu=1 \
     --memory=4GB \
     --region=us-central \
     --root-password=DB_ROOT_PASSWORD \
     --no-assign-ip \
    --network=default
  3. Run the gcloud sql instances patch command to enable only allow SSL connections for the instance.

  4. gcloud sql instances patch quickstart-instance --require-ssl
Create a database Console
  1. In the Google Cloud console, go to the Cloud SQL Instances page.

    Go to Cloud SQL Instances

  2. Select quickstart-instance.
  3. From the SQL navigation menu, select Databases.
  4. Click Create database.
    1. In the Database name field of the New database dialog box, enter quickstart-db.
    2. Click Create.
gcloud

Run the gcloud sql databases create command to create a database.

gcloud sql databases create quickstart-db --instance=quickstart-instance
Create a user Console
  1. In the Google Cloud console, go to the Cloud SQL Instances page.

    Go to Cloud SQL Instances

  2. To open the Overview page of an instance, click the instance name.
  3. Select Users from the SQL navigation menu.
  4. Click Add user account.
  5. On the Add a user account to instance instance_name page, add the following information:
  6. Click Add.
gcloud

Before running the following command, make the following replacements:

  1. PASSWORD with a password for your database user. Make a note of this for use in a later step of this quickstart.

Run the gcloud sql users create command to create the user.

gcloud sql users create quickstart-user \
--instance=quickstart-instance \
--password=PASSWORD

User name length limits are the same for Cloud SQL as for on-premises PostgreSQL.

Deploy sample app to Cloud Run Configure a Cloud Run service account

Configure the service account used by Cloud Run so that it has the

Cloud SQL Client

role with permissions to connect to Cloud SQL.

Console
  1. In the Google Cloud console, go to the IAM page.

    Go to IAM

  2. For the service account named Compute Engine default service account, click the pencil icon.
  3. Click ADD ANOTHER ROLE.
  4. Add the Role named Cloud SQL Client.
  5. Click Save.
gcloud
  1. Run the following gcloud command to get a list of your project's service accounts:
    gcloud iam service-accounts list
  2. Copy the EMAIL of the Compute Engine service account.
  3. Run the following command to add the Cloud SQL Client role to Compute Engine service account:
    gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \
      --member="serviceAccount:SERVICE_ACCOUNT_EMAIL" \
      --role="roles/cloudsql.client"
Configure a Cloud SQL sample app

With a Cloud SQL instance, database, and service account with client permissions, you can now configure a sample application to connect to your Cloud SQL instance.

Public IP For public IP paths, Cloud Run provides encryption and connects using the Cloud SQL Auth Proxy through Unix sockets. Go
  1. In Cloud Shell Editor, open the sample app's source code.

    Open Cloud Shell Editor

  2. In the Open in Cloud Shell dialog box, click Confirm to download the sample app code and open the sample app directory in Cloud Shell Editor.
  3. Run the following command in Cloud Shell to build a Docker container and publish it to Container Registry. Replace YOUR_PROJECT_ID with your project's project id.
    gcloud builds submit --tag gcr.io/YOUR_PROJECT_ID/run-sql
Java
  1. In Cloud Shell Editor, open the sample app's source code.

    Open Cloud Shell Editor

  2. In the Open in Cloud Shell dialog box, click Confirm to download the sample app code and open the sample app directory in Cloud Shell Editor.
  3. Run the following command in Cloud Shell to build a Docker container and publish it to Container Registry. Replace YOUR_PROJECT_ID with your project's project id.
    mvn clean package com.google.cloud.tools:jib-maven-plugin:2.8.0:build \
     -Dimage=gcr.io/YOUR_PROJECT_ID/run-sql -DskipTests
Node.js
  1. In Cloud Shell Editor, open the sample app's source code.

    Open Cloud Shell Editor

  2. In the Open in Cloud Shell dialog box, click Confirm to download the sample app code and open the sample app directory in Cloud Shell Editor.
  3. Run the following command in Cloud Shell to build a Docker container and publish it to Container Registry. Replace YOUR_PROJECT_ID with your project's project id.
    gcloud builds submit --tag gcr.io/YOUR_PROJECT_ID/run-sql
Python
  1. In Cloud Shell Editor, open the sample app's source code.

    Open Cloud Shell Editor

  2. In the Open in Cloud Shell dialog box, click Confirm to download the sample app code and open the sample app directory in Cloud Shell Editor.
  3. Run the following command in Cloud Shell to build a Docker container and publish it to Container Registry. Replace YOUR_PROJECT_ID with your project's project id.
    gcloud builds submit --tag gcr.io/YOUR_PROJECT_ID/run-sql
Private IP For private IP paths, your application connects directly to your instance through Serverless VPC Access. This method uses a TCP socket to connect directly to the Cloud SQL instance without using the Cloud SQL Auth Proxy. Go Create and download SSL server and client certificates
  1. In the Google Cloud console, go to the Cloud SQL Instances page.

    Go to Cloud SQL Instances

  2. Click the quickstart-instance to see its Overview page
  3. Click the Connections tab.
  4. Under the Security section, click Create client certificate.
  5. In the Create a client certificate dialog, enter quickstart-key as the name and click Create.
  6. In the New SSL certificate created dialog, click each download link to download the certificates. Then, click Close.

    Important: Store this private key securely. If you lose it, you must create a new client certificate.
Create a Serverless VPC Connection for connections to the instance via Private IP
  1. In the Google Cloud console, go to the Serverless VPC access - Create connector page.

    Create Serverless VPC connector

  2. Enter quickstart-connector for the Name.
  3. Select default from the Network drop-down menu
  4. Select Custom IP range from the Subnet drop-down menu
  5. Enter 10.8.0.0 in the IP range input box
  6. Click Create to create the connector.
Build sample app with SSL certificates
  1. In Cloud Shell Editor, open the sample app's source code.
    Open Cloud Shell Editor
  2. In the Open in Cloud Shell dialog, click Confirm to download the sample app code and open the sample app directory in Cloud Shell Editor.
  3. Upload SSL certs files to certs folder.
    1. Right-click certs folder in Cloud Shell Editor and select Upload Files
    2. Select following files on your local machine:
      • client-key.pem
      • client-cert.pem
      • server-ca.pem
    3. With the three SSL certificate files selected, click Open to complete the process of uploading the files to Cloud Shell Editor.
  4. Run the following command in Cloud Shell to build a Docker container and publish it to Container Registry. Replace YOUR_PROJECT_ID with your project's project id.
    gcloud builds submit --tag gcr.io/YOUR_PROJECT_ID/run-sql
Java Create and download SSL server and client certificates

For Java users, the connector already provides a secure connection so that creating and downloading SSL server and client certificates is unnecessary.

Create a Serverless VPC Connection for connections to the instance via Private IP
  1. In the Google Cloud console, go to the Serverless VPC access - Create connector page.

    Create Serverless VPC connector

  2. Enter quickstart-connector for the Name.
  3. Select default from the Network drop-down menu
  4. Select Custom IP range from the Subnet drop-down menu
  5. Enter 10.8.0.0 in the IP range input box
  6. Click Create to create the connector.
Build Sample App
  1. In Cloud Shell Editor, open the sample app's source code.
    Open Cloud Shell Editor
  2. In the Open in Cloud Shell dialog, click Confirm to download the sample app code and open the sample app directory in Cloud Shell Editor.
  3. Run the following command in Cloud Shell to build a Docker container and publish it to Container Registry. Replace YOUR_PROJECT_ID with your project's project id.
    mvn clean package com.google.cloud.tools:jib-maven-plugin:2.8.0:build \
     -Dimage=gcr.io/YOUR_PROJECT_ID/run-sql -DskipTests
Node.js Create and download SSL server and client certificates
  1. In the Google Cloud console, go to the Cloud SQL Instances page.

    Go to Cloud SQL Instances

  2. Click the quickstart-instance to see its Overview page
  3. Click the Connections tab.
  4. Under the Security section, click Create client certificate.
  5. In the Create a client certificate dialog, enter quickstart-key as the name and click Create.
  6. In the New SSL certificate created dialog, click each download link to download the certificates. Then, click Close.

    Important: Store this private key securely. If you lose it, you must create a new client certificate.
Create a Serverless VPC Connection for connections to the instance via Private IP
  1. In the Google Cloud console, go to the Serverless VPC access - Create connector page.

    Create Serverless VPC connector

  2. Enter quickstart-connector for the Name.
  3. Select default from the Network drop-down menu
  4. Select Custom IP range from the Subnet drop-down menu
  5. Enter 10.8.0.0 in the IP range input box
  6. Click Create to create the connector.
Build sample app with SSL certificates
  1. In Cloud Shell Editor, open the sample app's source code.
    Open Cloud Shell Editor
  2. In the Open in Cloud Shell dialog, click Confirm to download the sample app code and open the sample app directory in Cloud Shell Editor.
  3. Upload SSL certs files to certs folder.
    1. Right-click certs folder in Cloud Shell Editor and select Upload Files
    2. Select following files on your local machine:
      • client-key.pem
      • client-cert.pem
      • server-ca.pem
    3. With the three SSL certificate files selected, click Open to complete the process of uploading the files to Cloud Shell Editor.
  4. Run the following command in Cloud Shell to build a Docker container and publish it to Container Registry. Replace YOUR_PROJECT_ID with your project's project id.
    gcloud builds submit --tag gcr.io/YOUR_PROJECT_ID/run-sql
Python Create and download SSL server and client certificates
  1. In the Google Cloud console, go to the Cloud SQL Instances page.

    Go to Cloud SQL Instances

  2. Click the quickstart-instance to see its Overview page
  3. Click the Connections tab.
  4. Under the Security section, click Create client certificate.
  5. In the Create a client certificate dialog, enter quickstart-key as the name and click Create.
  6. In the New SSL certificate created dialog, click each download link to download the certificates. Then, click Close.

    Important: Store this private key securely. If you lose it, you must create a new client certificate.
Create a Serverless VPC Connection for connections to the instance via Private IP
  1. In the Google Cloud console, go to the Serverless VPC access - Create connector page.

    Create Serverless VPC connector

  2. Enter quickstart-connector for the Name.
  3. Select default from the Network drop-down menu
  4. Select Custom IP range from the Subnet drop-down menu
  5. Enter 10.8.0.0 in the IP range input box
  6. Click Create to create the connector.
Build sample app with SSL certificates
  1. In Cloud Shell Editor, open the sample app's source code.
    Open Cloud Shell Editor
  2. In the Open in Cloud Shell dialog, click Confirm to download the sample app code and open the sample app directory in Cloud Shell Editor.
  3. Upload SSL certs files to certs folder.
    1. Right-click certs folder in Cloud Shell Editor and select Upload Files
    2. Select following files on your local machine:
      • client-key.pem
      • client-cert.pem
      • server-ca.pem
    3. With the three SSL certificate files selected, click Open to complete the process of uploading the files to Cloud Shell Editor.
  4. Run the following command in Cloud Shell to build a Docker container and publish it to Container Registry. Replace YOUR_PROJECT_ID with your project's project id.
    gcloud builds submit --tag gcr.io/YOUR_PROJECT_ID/run-sql
Deploy the sample app

The steps to deploy the sample to Cloud Run depend on the type of IP address you assigned to your Cloud SQL instance.

The image connection method varies based on which environment variables are set.

Public IP Console Note: If you are using the Console to deploy a function, you must enable the required APIs, have the required roles granted to you, and click on the form for Write a function.
  1. In the Google Cloud console, go to the Cloud Run page.

    Go to Cloud Run

  2. Click Create container and select Service to display the Create service form.

  3. Click the Select button in the Container image URL input field.
    1. Select the gcr.io/YOUR_PROJECT_ID/run-sql container image you created in the previous step.
  4. In the Service name field, enter quickstart-service.
  5. In the Authentication section, select the Allow unauthenticated invocations option. If you don't have permissions (Cloud Run Admin role) to select this, the service will deploy and require authentication.
  6. Expand the Container, Variables & Secrets, Connections, Security section.
  7. Create the following Environment variables by clicking Add Variable under Environment variables. Set the values for the environment variables, as follows:
  8. Enable connecting to Cloud SQL:

  9. Click Create to create the Cloud Run service.

  10. After the Cloud Run service is deployed, the Service details page displays the URL of the running service at the top of the page. Click the URL link to see the deployed sample app on Cloud Run connected to Cloud SQL.

gcloud Note: If you are using the gcloud CLI to deploy a function, you must specify the required flags when running gcloud run deploy, and have the required roles granted to you.
  1. Before running the following command, make the following replacements:

  2. Run the gcloud run deploy command as follows to create the Cloud Run service. Environment variables vary depending on the connection method you want to use:

    gcloud run deploy run-sql --image gcr.io/YOUR_PROJECT_ID/run-sql \
        --add-cloudsql-instances INSTANCE_CONNECTION_NAME \
        --set-env-vars INSTANCE_CONNECTION_NAME="INSTANCE_CONNECTION_NAME" \
        --set-env-vars DB_NAME="quickstart-db" \
        --set-env-vars DB_USER="quickstart-user" \
        --set-env-vars DB_PASS="DB_PASS"

    Enter the numeric choice provided for us-central1 when prompted to specify a region.

  3. When you see a confirmation message that the Cloud Run service has been deployed, click the Service URL link in the message to see the sample app on Cloud Run that is connected to Cloud SQL.

Private IP Console Note: If you are using the Console to deploy a function, you must enable the required APIs, have the required roles granted to you, and click on the form for Write a function.
  1. In the Google Cloud console, go to the Cloud Run page.

    Go to Cloud Run

  2. Click Create container and select Service to display the Create service form.

  3. Retain the option to deploy from an existing container image and click Select to specify the gcr.io/YOUR_PROJECT_ID/run-sql container image you created in the previous step.
  4. Enter quickstart-service for the Service name.
  5. In the Authentication section, select the Allow unauthenticated invocations option. If you don't have permissions (Cloud Run Admin role) to select this, the service will deploy and require authentication.
  6. Expand the Container, Variables & Secrets, Connections, Security section.
  7. Create the following environment variables by clicking Add variable under Environment variables. Set the values for the environment variables, as follows:
  8. Enable connecting to Cloud SQL:

    1. Click Connections.
    2. Click Add Connection in the Cloud SQL connections section.
    3. Select the quickstart-instance Cloud SQL instance that you previously created.
    4. Select default: Serverless VPC Access Connector "quickstart-connector" from the VPC Network drop-down menu.
    5. Select the option Route all traffic through the VPC connector.
  9. Click Create to create the Cloud Run service.

  10. After the Cloud Run service is deployed, the Service details page displays the URL of the running service at the top of the page. Click the URL link to see the deployed sample app on Cloud Run connected to Cloud SQL.

gcloud Note: If you are using the gcloud CLI to deploy a function, you must specify the required flags when running gcloud run deploy, and have the required roles granted to you.
  1. Before running the following command, make the following replacements:

  2. Run the gcloud run deploy command as follows to create the Cloud Run service. Environment variables vary depending on the connection method you want to use:

    gcloud run deploy run-sql --image gcr.io/YOUR_PROJECT_ID/run-sql \
        --add-cloudsql-instances INSTANCE_CONNECTION_NAME \
        --vpc-connector="quickstart-connector" --vpc-egress=all-traffic \
        --set-env-vars DB_NAME="quickstart-db" \
        --set-env-vars DB_USER="quickstart-user" \
        --set-env-vars DB_PASS="DB_PASS" \
        --set-env-vars INSTANCE_CONNECTION_NAME="INSTANCE_CONNECTION_NAME"
      
    gcloud run deploy run-sql --image gcr.io/YOUR_PROJECT_ID/run-sql \
        --add-cloudsql-instances INSTANCE_CONNECTION_NAME \
        --vpc-connector="quickstart-connector" --vpc-egress=all-traffic \
        --set-env-vars DB_NAME="quickstart-db" \
        --set-env-vars DB_USER="quickstart-user" \
        --set-env-vars DB_PASS="DB_PASS" \
        --set-env-vars INSTANCE_CONNECTION_NAME="INSTANCE_CONNECTION_NAME"
      

    Enter the numeric choice provided for us-central1 when prompted to specify a region.

  3. When you see a confirmation message that the Cloud Run service has been deployed, click the Service URL link in the message to see the sample app on Cloud Run that is connected to Cloud SQL.

Clean up

To avoid incurring charges to your Google Cloud account for the resources used on this page, follow these steps.

  1. In the Google Cloud console, go to the Cloud SQL Instances page.

    Go to Cloud SQL Instances

  2. Select the quickstart-instance instance to open the Instance details page.
  3. In the icon bar at the top of the page, click Delete.
  4. In the Delete instance dialog box, type quickstart-instance, and then click Delete to delete the instance.
  5. In the Google Cloud console, go to the Cloud Run page.

    Go to Cloud Run

  6. Select the checkbox next to the quickstart-service service name.
  7. Click Delete at the top of the Cloud Run page.
Optional cleanup steps

If you're not using the Cloud SQL client role that you assigned to the Compute Engine service account, you can remove it.

  1. In the Google Cloud console, go to the IAM page.

    Go to IAM

  2. Click the edit icon (which looks like a pencil) for the IAM account named Compute Engine default service account.
  3. Delete the Cloud SQL client role.
  4. Click Save.

If you're not using the APIs that were enabled as part of this quickstart, you can disable them.

  1. In the Google Cloud console, go to the APIs page.

    Go to APIs

  2. Select any API that you would like to disable and then click the Disable API button.

What's next Based on your needs, you can learn more about creating Cloud SQL instances.

You also can learn about creating PostgreSQL users and databases for your Cloud SQL instance.

For more information about pricing, see Cloud SQL for PostgreSQL pricing.

Learn more about:

Additionally, you can learn about connecting to a Cloud SQL instance from other Google Cloud applications:


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