Connect to Cloud SQL for PostgreSQL from Compute Engine

Learn how to deploy a sample app on your Linux or Windows based Compute Engine VM instance 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).

Before you begin

  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.

    Go to project selector

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

  4. Enable the Cloud APIs necessary to run a Cloud SQL sample app on a Compute Engine VM instance.

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

    Enable APIs

    This enables the following API:

    • Cloud SQL Admin API

    Install the gcloud CLI which provides command-line access to your Google Cloud resources. The gcloud CLI is used to run the gcloud CLI commands presented throughout this quickstart. All the commands are formatted to be run in a terminal or a Powershell window.

    Run the following gcloud command:

    gcloud services enable sqladmin.googleapis.com

    This command enables the following API:

    • Cloud SQL Admin API

Set up Cloud SQL

Create a Cloud SQL instance

Public IP

Create an instance with a public IP address

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

    Go to Cloud SQL Instances

  2. Click Create instance.
  3. Click PostgreSQL.
  4. In the Instance ID field, enter quickstart-instance.
  5. In the Password field, enter a password for the postgres user. Save this password for future use.
  6. In the Choose region and zonal availability section, select Single zone.
  7. Expand the Show Configurations section.
  8. In the Machine Type drop-down list, select Lightweight.
  9. Click Create Instance and then wait until the instance initializes and starts.

Create an instance with a public IP address

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

Optionally, modify the values for the following parameters:

  • --database_version: The database engine type and version. If left unspecified, the API default is used. See the gcloud database versions documentation to see the currently available versions.
  • --cpu: The number of cores desired in the machine.
  • --memory: Whole number value indicating how much memory is desired in the machine. A size unit should be provided (for example, 3072MB or 9GB). If no units are specified, GB is assumed.
  • --region: Regional location of the instance (for example asia-east1, us-east1). If left unspecified, the default us-central is used. See the full list of regions.

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

gcloud sql instances create quickstart-instance --database-version=POSTGRES_13 --cpu=1 --memory=4GB --region=us-central --root-password=DB_ROOT_PASSWORD

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 PostgreSQL.
  4. Enter quickstart-instance for Instance ID.
  5. Enter a password for the postgres user. Save this password for future use.
  6. Click the Single zone option for Choose region and zonal availability.
  7. Click and expand Show configuration options.
  8. For Machine Type, select Lightweight.
  9. In Connections, select Private IP.
  10. Select default in the Network drop-down menu.
  11. Clear the Public IP checkbox to create an instance only with a private IP.
  12. Click Create instance and then wait for the instance to initialize and start.
  13. Click Connections.
  14. In the Security section, select Allow only SSL connections to enable SSL connections.
  15. In the Allow only SSL connections dialog, click Save and then wait for the instance to restart.

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:

    • --database_version: The database engine type and version. If left unspecified, the API default is used. See the gcloud database versions documentation to see the currently available versions.
    • --cpu: The number of cores in the machine.
    • --memory: A whole number value indicating how much memory to include in the machine. A size unit can be provided (for example, 3072MB or 9GB). If no units are specified, GB is assumed.
    • --region: The regional location of the instance (for example asia-east1, us-east1). If left unspecified, the default us-central1 is used. See the full list of regions.

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

    gcloud beta sql instances create quickstart-instance --database-version=POSTGRES_13 --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

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

    Go to Cloud SQL Instances

  2. Select quickstart-instance.
  3. Open the Databases tab.
  4. Click Create database.
    1. In the New database dialog box, enter quickstart_db as the name of the database.
    2. Click Create.

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

gcloud sql databases create quickstart_db --instance=quickstart-instance

Create a user

  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:
    • In the Username field, enter quickstart-user
    • In the Password field, specify a password for your database user. Make a note of this for use in a later step of this quickstart.
  6. Click Add.

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.

Configure a Compute Engine service account

Create and configure a Google Cloud service account that has the Cloud SQL Client role with permissions to connect to Cloud SQL. After you create a service account, you might need to wait for 60 seconds or more before you use the service account.

Create a service account

  1. In the Google Cloud console, go to the Create service account page.
  2. Go to Create service account

  3. Select a Google Cloud project.
  4. Enter a quickstart-service-account as the service account name.
  5. Optional: Enter a description of the service account.
  6. Click Create and continue and continue to the next step.
  7. Choose the Cloud SQL Client role to grant to the service account on the project.
  8. Click Add another role and choose the Storage Object Viewer role to grant to the service account on the project.
  9. Click Continue.
  10. Click Done to finish creating the service account.

Create a service account

  1. To create the service account, run the gcloud iam service-accounts create command:
  2. gcloud iam service-accounts create quickstart-service-account --description="DESCRIPTION" --display-name="quickstart-service-account"

    Replace the following value:

    • DESCRIPTION: an optional description of the service account
  3. To grant your service account the Cloud SQL Client role and the Storage Object Viewer role on your project, run the gcloud projects add-iam-policy-binding command. Replace PROJECT_ID with your Google Cloud project ID:
    gcloud projects add-iam-policy-binding PROJECT_ID --member="serviceAccount:quickstart-service-account@PROJECT_ID.iam.gserviceaccount.com" --role="roles/cloudsql.client" --role="roles/storage.objectViewer"

Create Compute Engine VM instance

Create a Compute Engine VM Instance to host a sample web app that connects to Cloud SQL.

Create a Linux VM Instance

  1. In the Google Cloud console, go to the VM instances page.

    Go to VM instances

  2. Click the Create instance button.
  3. For VM instance Name enter quickstart-vm-instance.
  4. For Service accounts select quickstart-service-account.
  5. For Firewall select the Allow HTTP traffic option.
  6. Click Create to create the VM instance.

Before running the following command, replace YOUR_PROJECT_ID with your project ID.

Run the following gcloud compute instances create command.

gcloud compute instances create quickstart-vm-instance --image-family=debian-10 --image-project=debian-cloud  --machine-type=e2-medium --service-account=quickstart-service-account@YOUR_PROJECT_ID.iam.gserviceaccount.com --scopes=https://www.googleapis.com/auth/cloud-platform --tags=http-server --zone=us-central1-a
  1. In the Google Cloud console, go to the VM instances page.

    Go to VM instances

  2. Click the Create instance button.
  3. For VM instance Name enter quickstart-vm-instance.
  4. For Boot disc click the Change button.
    1. For Operating system select Windows Server.
    2. For Version select Windows Server 2022 Datacenter.
    3. Click the Select button.
  5. For Service accounts select quickstart-service-account.
  6. For Firewall select the Allow HTTP traffic option.
  7. Click Create to create the VM instance.
  8. After 2-3 minutes once the VM instance has started, click the Set Windows Password button on the VM instance details page.
  9. Copy and save this password in a secure location as you will be using it to access your VM instance in the next step of this quickstart.

Before running the following command, replace YOUR_PROJECT_ID with your project ID.

Run the following gcloud compute instances create command in a Terminal Window.

gcloud compute instances create quickstart-vm-instance --image-project=windows-cloud --image-family=windows-2022 --machine-type=e2-medium --service-account=quickstart-service-account@YOUR_PROJECT_ID.iam.gserviceaccount.com --scopes=https://www.googleapis.com/auth/cloud-platform --tags=http-server --zone=us-central1-a

After 2-3 minutes once the VM instance has started, run the following command to set the Windows password on the VM instance.

gcloud compute reset-windows-password quickstart-vm-instance

Copy and save this password in a secure location as you will be using it to access your VM instance in the next step of this quickstart.

Access Compute Engine VM instance

Access Linux VM Instance

  • In the Google Cloud console, go to the VM instances page.

    Go to VM instances

  • In the list of virtual machine instances, click SSH in the row of the instance that you want to connect to.

    SSH button next to instance name.

  • Note: When you connect to VMs using the Google Cloud console, Compute Engine creates an ephemeral SSH key for you. For more information about SSH keys, see SSH connections to Linux VMs

    Use the gcloud compute ssh command to connect to a Linux VM instance. Replace YOUR_PROJECT_ID with your project ID:

    gcloud compute ssh --project=YOUR_PROJECT_ID --zone=us-central1-a quickstart-vm-instance

    Chrome RDP for Google Cloud is a third-party plugin that lets you connect to Windows instances by using the Chrome browser. The plugin is integrated with the Google Cloud console. After you install the plugin, connect to any Windows Server instance by using the RDP button in the Google Cloud console .

    To connect using the Chrome RDP plugin, do the following:

    1. Install the Chrome RDP for Google Cloud extension.
    2. In Google Cloud console, go to the VM instances page and find the Windows instance you want to connect to.
    3. Go to the VM instances page

    4. Click the RDP button for the instance you want to connect to. The Chrome RDP extension opens.
    5. Since your VM instance does not have a domain configured, you can leave the Domain field blank
    6. Enter your username, and password, and click OK to connect.
    7. If prompted, press Continue to accept the certificate.

    See Connect to Windows VMs using RDP for more options for accessing a Compute Engine Windows VM instance.

    Setup development environment for programming language

    Set up the Compute Engine VM instance's development environment for your preferred programming language.

    Setup Linux VM Instance development environment

    Complete the following steps to set up the Compute Engine VM instance's development environment to run the Go sample app.

    1. Go to the setup guide for a Go development environment.
    2. Complete the instructions in the Install Go section.

    Complete the following steps to set up the Compute Engine VM instance's development environment to run the Java sample app.

    1. Go to the setup guide for a Java development environment.
    2. Complete the instructions in the Install a JDK (Java Development Kit) section.
    3. Complete the instructions in the Install a build automation tool to set up Apache Maven.

    Complete the following steps to set up the Compute Engine VM instance's development environment to run the Node.js sample app.

    1. Go to setup guide for a Node.js development environment.
    2. Complete the instructions in the Installing Node.js and npm section.

    Complete the following steps to set up the Compute Engine VM instance's development environment to run the Python sample app.

    1. Go to setup guide for a Python development environment.
    2. Complete the instructions in the Installing Python section.

    Complete the following steps to set up the Compute Engine VM instance's development environment to run the Go sample app.

    1. Go to the setup guide for a Go development environment.
    2. Complete the instructions in the Install Go section.

    Complete the following steps to set up the Compute Engine VM instance's development environment to run the Java sample app.

    1. Go to the setup guide for a Java development environment.
    2. Complete the instructions in the Install a JDK (Java Development Kit) section.
    3. Complete the instructions in the Install a build automation tool section to set up Apache Maven.

    Complete the following steps to set up the Compute Engine VM instance's development environment to run the Node.js sample app.

    1. Go to setup guide for a Node.js development environment.
    2. Complete the instructions in the Installing Node.js and npm section.

    Complete the following steps to set up the Compute Engine VM instance's development environment to run the Python sample app.

    1. Install Python and the PIP package manager for Python.
      1. Go to setup guide for a Python development environment.
      2. Complete the instructions in the Installing Python section.
        1. The instructions above will have you visit the Python Releases for Windows download page. From that page click the Latest Python 3 Release link.
        2. On the Python 3.X.X page, click the Windows Installer 64-bit link to download the installer file to your Windows Compute Engine VM instance.
        3. Once you've downloaded the Python installer to the VM instance, open the folder containing the downloaded file. Then right click on the installer file and select Run as administrator.
        4. In the Install Python dialog that appears, select the option to Add Python 3.X.X to PATH and click → Install Now.
    2. Use PIP to install virtualenv.
      1. Open Powershell on the Windows Compute Engine VM instance and run the following pip install command.
      2. pip install virtualenv

    Install Git

    Install Git, an open source version control system on to your Compute Engine VM instance.

    Compute Engine Linux VM instance

    On your Compute Engine Linux VM instance, follow the official Git installation documentation for Linux.

    • Run the suggested Debian/Ubuntu install git command using the sudo command prefix to run the command as an administrator. The full installation command to run in the terminal should be formatted as follows:
    • sudo apt-get install git

    On your Compute Engine Windows VM instance, follow the official Git installation documentation for Windows to download the 64-bit Standalone Installer and run it to install Git.

    Clone sample app

    Clone a sample app to your Compute Engine VM instance using the git clone command.

    On your Compute Engine VM instance, open a new terminal or Powershell window. Run the following commands to clone the Go sample app and change the directory to the directory containing the sample app.

    1. Clone the sample app.
      git clone https://github.com/GoogleCloudPlatform/golang-samples
    2. Change directory to the directory containing the sample app.
      cd golang-samples/cloudsql/postgres/database-sql

    On your Compute Engine VM instance, open a new terminal or Powershell window. Run the following commands to clone the Java sample app and change the directory to the directory containing the sample app.

    1. Clone the sample app.
      git clone https://github.com/GoogleCloudPlatform/java-docs-samples
    2. Change directory to the directory containing the sample app.
      cd java-docs-samples/cloud-sql/postgres/servlet

    On your Compute Engine VM instance, open a new terminal or Powershell window. Run the following commands to clone the Node.js sample app and change the directory to the directory containing the sample app.

    1. Clone the sample app.
      git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples
    2. Change directory to the directory containing the sample app.
      cd nodejs-docs-samples/cloud-sql/postgres/knex

    On your Compute Engine VM instance, open a new terminal or Powershell window. Run the following commands to clone the Python sample app and change the directory to the directory containing the sample app.

    1. Clone the sample app.
      git clone https://github.com/GoogleCloudPlatform/python-docs-samples
    2. Change directory to the directory containing the sample app.
      cd python-docs-samples/cloud-sql/postgres/sqlalchemy

    Configure and run a Cloud SQL sample app

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

    Public IP Cloud SQL Instance and Linux based Compute Engine VM

    On the Compute Engine VM instance in the open terminal, run the following commands to initialize environment variables required to run the sample app. Before running the commands, make the following replacements:

    • INSTANCE_CONNECTION_NAME with your instance's Connection name that appears on the Cloud SQL instances page in the Google Cloud console.
    • YOUR_DB_PASSWORD with the password of the quickstart-user that you created in the previous Create a user quickstart step.
    export INSTANCE_CONNECTION_NAME='INSTANCE_CONNECTION_NAME'
    export DB_PORT='5432'
    export DB_NAME='quickstart_db'
    export DB_USER='quickstart-user'
    export DB_PASS='YOUR_DB_PASSWORD'

    On the Compute Engine VM instance in the open terminal, run the following commands to get the Go sample app's dependencies on to your Compute Engine VM instance and run the sample app.

    1. Get the dependencies required to run to sample app.
      go get ./...
    2. Run the sample app.
      go run cmd/app/main.go

    On your local computer get the Compute Engine VM instance's external IP address where the sample app is running and view it in a browser.

    1. In a terminal or in Powershell on your local computer, get the Compute Engine VM instance's external IP address by running the following gcloud compute instances describe command:
    2. gcloud compute instances describe quickstart-vm-instance --format="value(networkInterfaces[0].accessConfigs[].natIP)"
    3. View the running sample app. Open a browser on your local computer and go to the Compute Engine VM instance's external IP address and port :8080.
      http://COMPUTE_ENGINE_VM_EXTERNAL_IP_ADDRESS:8080

      View deployed sample app

    To stop the sample app, press Control+C in the Compute Engine VM instance terminal where you started the sample app.

    On the Compute Engine VM instance in the open terminal, run the following commands to initialize environment variables required to run the sample app. Before running the commands, make the following replacements:

    • INSTANCE_CONNECTION_NAME with your instance's Connection name that appears on the Cloud SQL instances page in the Google Cloud console.
    • YOUR_DB_PASSWORD with the password of the quickstart-user that you created in the previous Create a user quickstart step.
    export INSTANCE_CONNECTION_NAME='INSTANCE_CONNECTION_NAME'
    export DB_PORT='5432'
    export DB_NAME='quickstart_db'
    export DB_USER='quickstart-user'
    export DB_PASS='YOUR_DB_PASSWORD'

    On the Compute Engine VM instance in the open terminal, run the following command to get the Java sample app's dependencies on to your Compute Engine VM instance and run the sample app.

    mvn jetty:run

    On your local computer get the Compute Engine VM instance's external IP address where the sample app is running and view it in a browser.

    1. In a terminal or in Powershell on your local computer, get the Compute Engine VM instance's external IP address by running the following gcloud compute instances describe command:
    2. gcloud compute instances describe quickstart-vm-instance --format="value(networkInterfaces[0].accessConfigs[].natIP)"
    3. View the running sample app. Open a browser on your local computer and go to the Compute Engine VM instance's external IP address and port :8080.
      http://COMPUTE_ENGINE_VM_EXTERNAL_IP_ADDRESS:8080

      View deployed sample app

    To stop the sample app, press Control+C in the Compute Engine VM instance terminal where you started the sample app.

    1. On the Compute Engine VM instance in the open terminal, run the following commands to initialize environment variables required to run the sample app. Before running the commands, make the following replacement:

      • YOUR_DB_PASSWORD with the password of the quickstart-user that you created in the previous Create a user quickstart step.
      export INSTANCE_HOST='127.0.0.1'
      export DB_PORT='5432'
      export DB_NAME='quickstart_db'
      export DB_USER='quickstart-user'
      export DB_PASS='YOUR_DB_PASSWORD'
    2. Download the Cloud SQL Auth Proxy.
      curl -o cloud-sql-proxy \
      https://storage.googleapis.com/cloud-sql-connectors/cloud-sql-proxy/v2.0.0/cloud-sql-proxy.linux.amd64
    3. Set permissions to make the Cloud SQL Auth Proxy file executable.
      chmod +x cloud-sql-proxy
    4. Run the Cloud SQL Auth Proxy as a background process. Replace INSTANCE_CONNECTION_NAME with your instance's Connection name that appears on the Cloud SQL instances page in the Google Cloud console.
      ./cloud-sql-proxy INSTANCE_CONNECTION_NAME &

    On the Compute Engine VM instance in the open terminal, run the following commands to get the required Node.js packages on to your Compute Engine VM instance and run the sample app.

    1. Install the Node.js packages necessary to run the app locally.
      npm install
    2. Run the sample app.
      npm start

    On your local computer get the Compute Engine VM instance's external IP address where the sample app is running and view it in a browser.

    1. In a terminal or in Powershell on your local computer, get the Compute Engine VM instance's external IP address by running the following gcloud compute instances describe command:
    2. gcloud compute instances describe quickstart-vm-instance --format="value(networkInterfaces[0].accessConfigs[].natIP)"
    3. View the running sample app. Open a browser on your local computer and go to the Compute Engine VM instance's external IP address and port :8080.
      http://COMPUTE_ENGINE_VM_EXTERNAL_IP_ADDRESS:8080

      View deployed sample app

    To stop the sample app, press Control+C in the Compute Engine VM instance terminal where you started the sample app.

    On the Compute Engine VM instance in the open terminal, run the following commands to initialize environment variables required to run the sample app. Before running the commands, make the following replacements:

    • INSTANCE_CONNECTION_NAME with your instance's Connection name that appears on the Cloud SQL instances page in the Google Cloud console.
    • YOUR_DB_PASSWORD with the password of the quickstart-user that you created in the previous Create a user quickstart step.
    export INSTANCE_CONNECTION_NAME='INSTANCE_CONNECTION_NAME'
    export DB_PORT='5432'
    export DB_NAME='quickstart_db'
    export DB_USER='quickstart-user'
    export DB_PASS='YOUR_DB_PASSWORD'

    On the Compute Engine VM instance in the open terminal, run the following commands to get the Python sample app's requirements on to your Compute Engine VM instance and run the sample app.

    1. Initialize a virtual environment and install the requirements to run to sample app.
      python3 -m venv env
      source env/bin/activate
      pip install -r requirements.txt
    2. Run the sample app.
      python app.py

    On your local computer, set up port forwarding over SSH by performing the instructions in next quickstart step. This enables you to use a browser on your local computer to view the app running on your Compute Engine VM instance.

    1. In a terminal or in Powershell on your local computer, run the following gcloud compute ssh command to setup port forwarding over SSH. Before running the command, replace YOUR_PROJECT_ID with your project ID.
    2. gcloud compute ssh quickstart-vm-instance --project=YOUR_PROJECT_ID --zone=us-central1-a --ssh-flag='-L 8000:127.0.0.1:8080'
    3. View the running sample app. With port forwarding actively running, open a browser on your local computer and enter http://127.0.0.1:8000 in the address bar of your browser..

      View deployed sample app

    To stop the sample app, press Control+C in the Compute Engine VM instance terminal where you started the sample app.

    On the Compute Engine VM instance in the open Powershell window, run the following commands to initialize environment variables required to run the sample app. Before running the commands, make the following replacements:

    • INSTANCE_CONNECTION_NAME with your instance's Connection name that appears on the Cloud SQL instances page in the Google Cloud console.
    • YOUR_DB_PASSWORD with the password of the quickstart-user that you created in the previous Create a user quickstart step.
    $env:INSTANCE_CONNECTION_NAME="INSTANCE_CONNECTION_NAME"
    $env:DB_PORT="5432"
    $env:DB_NAME="quickstart_db"
    $env:DB_USER="quickstart-user"
    $env:DB_PASS="YOUR_DB_PASSWORD"

    On the Compute Engine VM instance in the open Powershell window, run the following commands to get the Go sample app's dependencies on to your Compute Engine VM instance and run the sample app.

    1. Get the dependencies required to run to sample app.
      go get ./...
    2. Run the sample app.
      go run cmd\app\main.go
    3. View the running sample app. Open a browser on the Compute Engine VM instance and go to http://127.0.0.1:8080.

      View deployed sample app

    To stop the sample app, press Control+C in the Compute Engine VM instance Powershell window where you started the sample app.

    On the Compute Engine VM instance in the open Powershell window, run the following commands to initialize environment variables required to run the sample app. Before running the commands, make the following replacements:

    • INSTANCE_CONNECTION_NAME with your instance's Connection name that appears on the Cloud SQL instances page in the Google Cloud console.
    • YOUR_DB_PASSWORD with the password of the quickstart-user that you created in the previous Create a user quickstart step.
    $env:INSTANCE_CONNECTION_NAME="INSTANCE_CONNECTION_NAME"
    $env:DB_PORT="5432"
    $env:DB_NAME="quickstart_db"
    $env:DB_USER="quickstart-user"
    $env:DB_PASS="YOUR_DB_PASSWORD"
    1. On the Compute Engine VM instance in the open Powershell window, run the following command to get the Java sample app's dependencies on to your Compute Engine VM instance and run the sample app.

      mvn jetty:run
    2. View the running sample app. Open a browser on the Compute Engine VM instance and go to http://127.0.0.1:8080.

      View deployed sample app

    To stop the sample app, press Control+C in the Compute Engine VM instance Powershell window where you started the sample app.

    1. On the Compute Engine VM instance in the open Powershell window, run the following commands to initialize environment variables required to run the sample app. Before running the commands, make the following replacement:

      • YOUR_DB_PASSWORD with the password of the quickstart-user that you created in the previous Create a user quickstart step.
      $env:INSTANCE_HOST="127.0.0.1"
      $env:DB_PORT="5432"
      $env:DB_NAME="quickstart_db"
      $env:DB_USER="quickstart-user"
      $env:DB_PASS="YOUR_DB_PASSWORD"
    2. Download the Cloud SQL Auth Proxy.
      wget https://storage.googleapis.com/cloud-sql-connectors/cloud-sql-proxy/v2.0.0/cloud-sql-proxy.x64.exe `
      -O cloud-sql-proxy.exe
    3. Run the Cloud SQL Auth Proxy as a background process. Replace INSTANCE_CONNECTION_NAME with your instance's Connection name that appears on the Cloud SQL instances page in the Google Cloud console.
      Start-Process -filepath  ".\cloud-sql-proxy.exe" -ArgumentList `
      "INSTANCE_CONNECTION_NAME"

    On the Compute Engine VM instance in the open Powershell window, run the following commands to get the required Node.js packages on to your Compute Engine VM instance and run the sample app.

    1. Install the Node.js packages necessary to run the app locally.
      npm install
    2. Run the sample app.
      npm start
    3. View the running sample app. Open a browser on the Compute Engine VM instance and go to http://127.0.0.1:8080.

      View deployed sample app

    To stop the sample app, press Control+C in the Compute Engine VM instance Powershell window where you started the sample app.

    On the Compute Engine VM instance in the open Powershell window, run the following commands to initialize environment variables required to run the sample app. Before running the commands, make the following replacements:

    • INSTANCE_CONNECTION_NAME with your instance's Connection name that appears on the Cloud SQL instances page in the Google Cloud console.
    • YOUR_DB_PASSWORD with the password of the quickstart-user that you created in the previous Create a user quickstart step.
    $env:INSTANCE_CONNECTION_NAME="INSTANCE_CONNECTION_NAME"
    $env:DB_PORT="5432"
    $env:DB_NAME="quickstart_db"
    $env:DB_USER="quickstart-user"
    $env:DB_PASS="YOUR_DB_PASSWORD"

    On the Compute Engine VM instance in the open Powershell window, run the following commands to get the Python sample app's requirements on to your Compute Engine VM instance and run the sample app.

    1. Initialize a virtual environment and install the requirements to run to sample app.
      virtualenv --python python3 env
      .\env\Scripts\activate
      pip install -r requirements.txt
    2. Run the sample app.
      python app.py
    3. View the running sample app. Open a browser on the Compute Engine VM instance and go to http://127.0.0.1:8080.

      View deployed sample app

    To stop the sample app, press Control+C in the Compute Engine VM instance Powershell window where you started the sample app.

    Create and download SSL server and client certificates on to your local computer

    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.

    Upload SSL certificates to Cloud Storage bucket

    In a browser on your local computer, create a Cloud Storage bucket and upload SSL certificates to the bucket where they can then be accessed from the Compute Engine VM instance.

    1. Create a Cloud Storage bucket.
      1. For Name of your bucket, enter the following name. Replace YOUR_PROJECT_ID with your project ID:
        YOUR_PROJECT_ID-quickstart-certs
      2. Click the Create button to create the bucket.
    2. Click the Upload Files button to upload files to the newly created Cloud Storage bucket.
    3. Select the following files to be uploaded from your local computer to Cloud Storage:
      • server-ca.pem
      • client-cert.pem
      • client-key.pem

    On your local computer in a terminal or Powershell window open to the directory where you downloaded the SSL certificates, you can now create a Cloud Storage bucket and upload the SSL certificates to bucket where they can then be accessed from the Compute Engine VM instance. The gcloud CLI will be used to upload the files.

    1. Run the following gcloud storage buckets create command to make a new Cloud Storage bucket:
    2. gcloud storage buckets create gs://YOUR_PROJECT_ID-quickstart-certs --location=us-central1
    3. From the directory where you downloaded the certificates on your local computer, run the following gcloud storage cp commands to copy the SSL certificates to the newly created Cloud Storage bucket:
    4. gcloud storage cp server-ca.pem gs://YOUR_PROJECT_ID-quickstart-certs/
      gcloud storage cp client-cert.pem gs://YOUR_PROJECT_ID-quickstart-certs/
      gcloud storage cp client-key.pem gs://YOUR_PROJECT_ID-quickstart-certs/

    Download SSL certificates using the gcloud CLI on the Compute Engine VM instance

    On the Compute Engine VM instance in a terminal open to the golang-samples/cloudsql/postgres/database-sql directory, run the following gcloud storage cp commands to download the SSL certificates from Cloud Storage to the certs directory.

    gcloud storage cp gs://YOUR_PROJECT_ID-quickstart-certs/server-ca.pem certs/.
    gcloud storage cp gs://YOUR_PROJECT_ID-quickstart-certs/client-cert.pem certs/.
    gcloud storage cp gs://YOUR_PROJECT_ID-quickstart-certs/client-key.pem certs/.

    Set Environment Variables on the Compute Engine VM instance

    On the Compute Engine VM instance in the open terminal, run the following commands to initialize environment variables required to run the sample app. Before running the commands, make the following replacements:

    • INSTANCE_HOST set to the Private IP address of your instance displayed on the Cloud SQL instances page in the Google Cloud console.
    • YOUR_DB_PASSWORD with the password of the quickstart-user that you created in the previous Create a user quickstart step.
    export INSTANCE_HOST='INSTANCE_HOST'
    export DB_PORT='5432'
    export DB_NAME='quickstart_db'
    export DB_USER='quickstart-user'
    export DB_PASS='YOUR_DB_PASSWORD'
    export DB_ROOT_CERT='certs/server-ca.pem'
    export DB_CERT='certs/client-cert.pem'
    export DB_KEY='certs/client-key.pem'
    export PRIVATE_IP='TRUE'

    On the Compute Engine VM instance in the open terminal, run the following commands to get the Go sample app's dependencies on to your Compute Engine VM instance and run the sample app.

    1. Get the dependencies required to run to sample app.
      go get ./...
    2. Run the sample app.
      go run cmd/app/main.go

    On your local computer get the Compute Engine VM instance's external IP address where the sample app is running and view it in a browser.

    1. In a terminal or in Powershell on your local computer, get the Compute Engine VM instance's external IP address by running the following gcloud compute instances describe command:
    2. gcloud compute instances describe quickstart-vm-instance --format="value(networkInterfaces[0].accessConfigs[].natIP)"
    3. View the running sample app. Open a browser on your local computer and go to the Compute Engine VM instance's external IP address and port :8080.
      http://COMPUTE_ENGINE_VM_EXTERNAL_IP_ADDRESS:8080

      View deployed sample app

    To stop the sample app, press Control+C in the Compute Engine VM instance terminal where you started the sample app.

    Download SSL certificates using the gcloud CLI on the Compute Engine VM instance

    On the Compute Engine VM instance in a terminal open to the java-docs-samples/cloud-sql/postgres/servlet directory, run the following gcloud storage cp commands to download the SSL certificates from Cloud Storage to the current directory.

    gcloud storage cp gs://YOUR_PROJECT_ID-quickstart-certs/server-ca.pem .
    gcloud storage cp gs://YOUR_PROJECT_ID-quickstart-certs/client-cert.pem .
    gcloud storage cp gs://YOUR_PROJECT_ID-quickstart-certs/client-key.pem .

    Configure the SSL certificates for use in Java on the Compute Engine VM instance

    1. In the terminal on the Compute Engine VM instance, run the following command to convert the downloaded PEM certificate and key to a PKCS12 archive using openssl. Before running the following command, replace SSL_CLIENT_KEY_PASSWD with your own custom keystore password to be used to create the Java client keystore.
    2. openssl pkcs12 -export -in client-cert.pem -inkey client-key.pem \
      -name "postgresclient" -passout pass:SSL_CLIENT_KEY_PASSWD \
      -out client-keystore.p12

    Set Environment Variables on the Compute Engine VM instance

    On the Compute Engine VM instance in the open terminal, run the following commands to initialize environment variables required to run the sample app. Before running the commands, make the following replacements:

    • INSTANCE_HOST set to the Private IP address of your instance displayed on the Cloud SQL instances page in the Google Cloud console.
    • SSL_CLIENT_KEY_PASSWD with the password you specified for creating the Java client keystore with openssl in the previous quickstart step.
    • YOUR_DB_PASSWORD with the password of the quickstart-user that you created in the previous Create a user quickstart step.
    export INSTANCE_HOST='INSTANCE_HOST'
    export DB_PORT='5432'
    export DB_NAME='quickstart_db'
    export DB_USER='quickstart-user'
    export DB_PASS='YOUR_DB_PASSWORD'
    export SSL_CLIENT_KEY_PATH='client-keystore.p12'
    export SSL_CLIENT_KEY_PASSWD='SSL_CLIENT_KEY_PASSWD'
    export SSL_SERVER_CA_PATH='server-ca.pem'
    export PRIVATE_IP='TRUE'

    On the Compute Engine VM instance in the open terminal, run the following command to get the Java sample app's dependencies on to your Compute Engine VM instance and run the sample app.

    mvn jetty:run

    On your local computer get the Compute Engine VM instance's external IP address where the sample app is running and view it in a browser.

    1. In a terminal or in Powershell on your local computer, get the Compute Engine VM instance's external IP address by running the following gcloud compute instances describe command:
    2. gcloud compute instances describe quickstart-vm-instance --format="value(networkInterfaces[0].accessConfigs[].natIP)"
    3. View the running sample app. Open a browser on your local computer and go to the Compute Engine VM instance's external IP address and port :8080.
      http://COMPUTE_ENGINE_VM_EXTERNAL_IP_ADDRESS:8080

      View deployed sample app

    To stop the sample app, press Control+C in the Compute Engine VM instance terminal where you started the sample app.

    Download SSL certificates using the gcloud CLI on the Compute Engine VM instance

    On the Compute Engine VM instance in a terminal open to the nodejs-docs-samples/cloud-sql/postgres/knex directory, run the following gcloud storage cp commands to download the SSL certificates from Cloud Storage to the certs directory.

    gcloud storage cp gs://YOUR_PROJECT_ID-quickstart-certs/server-ca.pem certs/.
    gcloud storage cp gs://YOUR_PROJECT_ID-quickstart-certs/client-cert.pem certs/.
    gcloud storage cp gs://YOUR_PROJECT_ID-quickstart-certs/client-key.pem certs/.

    Set Environment Variables on the Compute Engine VM instance

    On the Compute Engine VM instance in the open terminal, run the following commands to initialize environment variables required to run the sample app. Before running the commands, make the following replacements:

    • INSTANCE_HOST set to the Private IP address of your instance displayed on the Cloud SQL instances page in the Google Cloud console.
    • YOUR_DB_PASSWORD with the password of the quickstart-user that you created in the previous Create a user quickstart step.
    export INSTANCE_HOST='INSTANCE_HOST'
    export DB_PORT='5432'
    export DB_NAME='quickstart_db'
    export DB_USER='quickstart-user'
    export DB_PASS='YOUR_DB_PASSWORD'
    export DB_ROOT_CERT='certs/server-ca.pem'
    export DB_CERT='certs/client-cert.pem'
    export DB_KEY='certs/client-key.pem'
    export PRIVATE_IP='TRUE'

    On the Compute Engine VM instance in the open terminal, run the following commands to get the required Node.js packages on to your Compute Engine VM instance and run the sample app.

    1. Install the Node.js packages necessary to run the app locally.
      npm install
    2. Run the sample app.
      npm start

    On your local computer get the Compute Engine VM instance's external IP address where the sample app is running and view it in a browser.

    1. In a terminal or in Powershell on your local computer, get the Compute Engine VM instance's external IP address by running the following gcloud compute instances describe command:
    2. gcloud compute instances describe quickstart-vm-instance --format="value(networkInterfaces[0].accessConfigs[].natIP)"
    3. View the running sample app. Open a browser on your local computer and go to the Compute Engine VM instance's external IP address and port :8080.
      http://COMPUTE_ENGINE_VM_EXTERNAL_IP_ADDRESS:8080

      View deployed sample app

    To stop the sample app, press Control+C in the Compute Engine VM instance terminal where you started the sample app.

    Download SSL certificates using the gcloud CLI on the Compute Engine VM instance

    On the Compute Engine VM instance in a terminal open to the python-docs-samples/cloud-sql/postgres/sqlalchemy directory, run the following gcloud storage cp commands to download the SSL certificates from Cloud Storage to the certs directory.

    gcloud storage cp gs://YOUR_PROJECT_ID-quickstart-certs/server-ca.pem certs/.
    gcloud storage cp gs://YOUR_PROJECT_ID-quickstart-certs/client-cert.pem certs/.
    gcloud storage cp gs://YOUR_PROJECT_ID-quickstart-certs/client-key.pem certs/.

    Set Environment Variables on the Compute Engine VM instance

    On the Compute Engine VM instance in the open terminal, run the following commands to initialize environment variables required to run the sample app. Before running the commands, make the following replacements:

    • INSTANCE_HOST set to the Private IP address of your instance displayed on the Cloud SQL instances page in the Google Cloud console.
    • YOUR_DB_PASSWORD with the password of the quickstart-user that you created in the previous Create a user quickstart step.
    export INSTANCE_HOST='INSTANCE_HOST'
    export DB_PORT='5432'
    export DB_NAME='quickstart_db'
    export DB_USER='quickstart-user'
    export DB_PASS='YOUR_DB_PASSWORD'
    export DB_ROOT_CERT='certs/server-ca.pem'
    export DB_CERT='certs/client-cert.pem'
    export DB_KEY='certs/client-key.pem'
    export PRIVATE_IP='TRUE'

    On the Compute Engine VM instance in the open terminal, run the following commands to get the Python sample app's requirements on to your Compute Engine VM instance and run the sample app.

    1. Initialize a virtual environment and install the requirements to run to sample app.
      python3 -m venv env
      source env/bin/activate
      pip install -r requirements.txt
    2. Run the sample app.
      python app.py

    On your local computer, set up port forwarding over SSH by performing the instructions in next quickstart step. This enables you to use a browser on your local computer to view the app running on your Compute Engine VM instance.

    1. In a terminal or in Powershell on your local computer, run the following gcloud compute ssh command to setup port forwarding over SSH. Before running the command, replace YOUR_PROJECT_ID with your project ID.
    2. gcloud compute ssh quickstart-vm-instance --project=YOUR_PROJECT_ID --zone=us-central1-a --ssh-flag='-L 8000:127.0.0.1:8080'
    3. View the running sample app. With port forwarding actively running, open a browser on your local computer and enter http://127.0.0.1:8000 in the address bar of your browser.

      View deployed sample app

    To stop the sample app, press Control+C in the Compute Engine VM instance terminal where you started the sample app.

    Create and download SSL server and client certificates on to your local computer

    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.

    Upload SSL certificates to Cloud Storage bucket

    In a browser on your local computer, create a Cloud Storage bucket and upload SSL certificates to the bucket where they can then be accessed from the Compute Engine VM instance.

    1. Create a Cloud Storage bucket.
      1. For Name of your bucket, enter the following name. Replace YOUR_PROJECT_ID with your project ID:
        YOUR_PROJECT_ID-quickstart-certs
      2. Click the Create button to create the bucket.
    2. Click the Upload Files button to upload files to the newly created Cloud Storage bucket.
    3. Select the following files to be uploaded from your local computer to Cloud Storage:
      • server-ca.pem
      • client-cert.pem
      • client-key.pem

    On your local computer in a terminal or Powershell window open to the directory where you downloaded the SSL certificates, you can now create a Cloud Storage bucket and upload the SSL certificates to bucket where they can then be accessed from the Compute Engine VM instance. The gcloud CLI will be used to upload the files.

    1. Run the following gcloud storage buckets create command to make a new Cloud Storage bucket:
    2. gcloud storage buckets create gs://YOUR_PROJECT_ID-quickstart-certs --location=us-central1
    3. From the directory where you downloaded the certificates on your local computer, run the following gcloud storage cp commands to copy the SSL certificates to the newly created Cloud Storage bucket:
    4. gcloud storage cp server-ca.pem gs://YOUR_PROJECT_ID-quickstart-certs/
      gcloud storage cp client-cert.pem gs://YOUR_PROJECT_ID-quickstart-certs/
      gcloud storage cp client-key.pem gs://YOUR_PROJECT_ID-quickstart-certs/

    Download SSL certificates using the gcloud CLI on the Compute Engine VM instance

    On the Compute Engine VM instance in a Powershell window open to the golang-samples/cloudsql/postgres/database-sql directory, run the following gcloud storage cp commands to download the SSL certificates from Cloud Storage to the certs directory.

    gcloud storage cp gs://YOUR_PROJECT_ID-quickstart-certs/server-ca.pem certs/.
    gcloud storage cp gs://YOUR_PROJECT_ID-quickstart-certs/client-cert.pem certs/.
    gcloud storage cp gs://YOUR_PROJECT_ID-quickstart-certs/client-key.pem certs/.

    Set Environment Variables on the Compute Engine VM instance

    On the Compute Engine VM instance in the Powershell window, run the following commands to initialize environment variables required to run the sample app. Before running the commands, make the following replacements:

    • INSTANCE_HOST set to the Private IP address of your instance displayed on the Cloud SQL instances page in the Google Cloud console.
    • YOUR_DB_PASSWORD with the password of the quickstart-user that you created in the previous Create a user quickstart step.
    $env:INSTANCE_HOST="INSTANCE_HOST"
    $env:DB_PORT="5432"
    $env:DB_NAME="quickstart_db"
    $env:DB_USER="quickstart-user"
    $env:DB_PASS="YOUR_DB_PASSWORD"
    $env:DB_ROOT_CERT="certs/server-ca.pem"
    $env:DB_CERT="certs/client-cert.pem"
    $env:DB_KEY="certs/client-key.pem"
    $env:PRIVATE_IP="TRUE"

    On the Compute Engine VM instance in the open Powershell window, run the following commands to get the Go sample app's dependencies on to your Compute Engine VM instance and run the sample app.

    1. Get the dependencies required to run to sample app.
      go get ./...
    2. Run the sample app.
      go run cmd\app\main.go
    3. View the running sample app. Open a browser on the Compute Engine VM instance and go to http://127.0.0.1:8080.

      View deployed sample app

    To stop the sample app, press Control+C in the Compute Engine VM instance Powershell window where you started the sample app.

    Download SSL certificates using the gcloud CLI on the Compute Engine VM instance

    On the Compute Engine VM instance in a Powershell window open to the java-docs-samples/cloud-sql/postgres/servlet directory, run the following gcloud storage cp commands to download the SSL certificates from Cloud Storage to the current directory.

    gcloud storage cp gs://YOUR_PROJECT_ID-quickstart-certs/server-ca.pem .
    gcloud storage cp gs://YOUR_PROJECT_ID-quickstart-certs/client-cert.pem .
    gcloud storage cp gs://YOUR_PROJECT_ID-quickstart-certs/client-key.pem .

    Configure the SSL certificates for use in Java on the Compute Engine VM instance

    1. In the terminal on the Compute Engine VM instance, run the following command to convert the downloaded PEM certificate and key to a PKCS12 archive using openssl. Before running the following command, replace SSL_CLIENT_KEY_PASSWD with your own custom keystore password to be used to create the Java client keystore.
    2. openssl pkcs12 -export -in client-cert.pem -inkey client-key.pem `
      -name "postgresclient" -passout pass:SSL_CLIENT_KEY_PASSWD `
      -out client-keystore.p12

    Set Environment Variables on the Compute Engine VM instance

    On the Compute Engine VM instance in the Powershell window, run the following commands to initialize environment variables required to run the sample app. Before running the commands, make the following replacements:

    • INSTANCE_HOST set to the Private IP address of your instance displayed on the Cloud SQL instances page in the Google Cloud console.
    • YOUR_DB_PASSWORD with the password of the quickstart-user that you created in the previous Create a user quickstart step.
    • SSL_CLIENT_KEY_PASSWD with the password you specified for SSL_CLIENT_KEY_PASSWD.
    $env:INSTANCE_HOST="INSTANCE_HOST"
    $env:DB_PORT="5432"
    $env:DB_NAME="quickstart_db"
    $env:DB_USER="quickstart-user"
    $env:DB_PASS="YOUR_DB_PASSWORD"
    $env:SSL_CLIENT_KEY_PATH="client-keystore.p12"
    $env:SSL_CLIENT_KEY_PASSWD="SSL_CLIENT_KEY_PASSWD"
    $env:SSL_SERVER_CA_PATH="server-ca.pem"
    $env:PRIVATE_IP="TRUE"
    1. On the Compute Engine VM instance in the open Powershell window, run the following command to get the Java sample app's dependencies on to your Compute Engine VM instance and run the sample app.

      mvn jetty:run
    2. View the running sample app. Open a browser on the Compute Engine VM instance and go to http://127.0.0.1:8080.

      View deployed sample app

    To stop the sample app, press Control+C in the Compute Engine VM instance Powershell window where you started the sample app.

    Download SSL certificates using the gcloud CLI on the Compute Engine VM instance

    On the Compute Engine VM instance in a Powershell window open to the nodejs-docs-samples/cloud-sql/postgres/knex directory, run the following gcloud storage cp commands to download the SSL certificates from Cloud Storage to the certs directory.

    gcloud storage cp gs://YOUR_PROJECT_ID-quickstart-certs/server-ca.pem certs/.
    gcloud storage cp gs://YOUR_PROJECT_ID-quickstart-certs/client-cert.pem certs/.
    gcloud storage cp gs://YOUR_PROJECT_ID-quickstart-certs/client-key.pem certs/.

    Set Environment Variables on the Compute Engine VM instance

    On the Compute Engine VM instance in the Powershell window, run the following commands to initialize environment variables required to run the sample app. Before running the commands, make the following replacements:

    • INSTANCE_HOST set to the Private IP address of your instance displayed on the Cloud SQL instances page in the Google Cloud console.
    • YOUR_DB_PASSWORD with the password of the quickstart-user that you created in the previous Create a user quickstart step.
    $env:INSTANCE_HOST="INSTANCE_HOST"
    $env:DB_PORT="5432"
    $env:DB_NAME="quickstart_db"
    $env:DB_USER="quickstart-user"
    $env:DB_PASS="YOUR_DB_PASSWORD"
    $env:DB_ROOT_CERT="certs/server-ca.pem"
    $env:DB_CERT="certs/client-cert.pem"
    $env:DB_KEY="certs/client-key.pem"
    $env:PRIVATE_IP="TRUE"

    On the Compute Engine VM instance in the open Powershell window, run the following commands to get the required Node.js packages on to your Compute Engine VM instance and run the sample app.

    1. Install the Node.js packages necessary to run the app locally.
      npm install
    2. Run the sample app.
      npm start
    3. View the running sample app. Open a browser on the Compute Engine VM instance and go to http://127.0.0.1:8080.

      View deployed sample app

    To stop the sample app, press Control+C in the Compute Engine VM instance Powershell window where you started the sample app.

    Download SSL certificates using the gcloud CLI on the Compute Engine VM instance

    On the Compute Engine VM instance in a Powershell window open to the python-docs-samples/cloud-sql/postgres/sqlalchemy directory, run the following gcloud storage cp commands to download the SSL certificates from Cloud Storage to the certs directory.

    gcloud storage cp gs://YOUR_PROJECT_ID-quickstart-certs/server-ca.pem certs/.
    gcloud storage cp gs://YOUR_PROJECT_ID-quickstart-certs/client-cert.pem certs/.
    gcloud storage cp gs://YOUR_PROJECT_ID-quickstart-certs/client-key.pem certs/.

    Set Environment Variables on the Compute Engine VM instance

    On the Compute Engine VM instance in the Powershell window, run the following commands to initialize environment variables required to run the sample app. Before running the commands, make the following replacements:

    • INSTANCE_HOST set to the Private IP address of your instance displayed on the Cloud SQL instances page in the Google Cloud console.
    • YOUR_DB_PASSWORD with the password of the quickstart-user that you created in the previous Create a user quickstart step.
    $env:INSTANCE_HOST="INSTANCE_HOST"
    $env:DB_PORT="5432"
    $env:DB_NAME="quickstart_db"
    $env:DB_USER="quickstart-user"
    $env:DB_PASS="YOUR_DB_PASSWORD"
    $env:DB_ROOT_CERT="certs/server-ca.pem"
    $env:DB_CERT="certs/client-cert.pem"
    $env:DB_KEY="certs/client-key.pem"
    $env:PRIVATE_IP="TRUE"

    On the Compute Engine VM instance in the open Powershell window, run the following commands to get the Python sample app's requirements on to your Compute Engine VM instance and run the sample app.

    1. Initialize a virtual environment and install the requirements to run to sample app.
      virtualenv --python python3 env
      .\env\Scripts\activate
      pip install -r requirements.txt
    2. Run the sample app.
      python app.py
    3. View the running sample app. Open a browser on the Compute Engine VM instance and go to http://127.0.0.1:8080.

      View deployed sample app

    To stop the sample app, press Control+C in the Compute Engine VM instance Powershell window where you started the sample app.

    Clean up

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

    Delete Cloud SQL instance

    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.

    Delete Compute Engine VM instance

    1. In the Google Cloud console, go to the VM instances page.

      Go to VM instances

    2. Select the quickstart-vm-instance instance to open the Instance details page.
    3. In the icon bar at the top of the page, click Delete.
    4. Click Delete to delete the instance.

    Optional cleanup steps

    If you're not using the Cloud SQL client role that you assigned to the Compute Engine default 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 API that was enabled as part of this quickstart, you can disable it.

    • API that was enabled within this quickstart:
      • Cloud SQL Admin API
    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:

    • Configuring your Cloud SQL instance with a public IP address.
    • Configuring your Cloud SQL instance with a private IP address.

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