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 instanceconnected to a PostgreSQL instance by using the Google Cloud console and a clientapplication.
Assuming that you complete all the steps in a timely manner, the resourcescreated in this quickstart typically cost less than one dollar (USD).
Before you begin
Note:The name you use for your project must be between 4 and 30 characters. When youtype the name, the form suggests a project ID, which you can edit. Theproject ID must be between 6 and 30 characters, with a lowercase letteras the first character. You can use a dash, lowercase letter, or digit for theremaining characters, but the last character cannot be a dash.- 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.
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.Make sure that billing is enabled for your Google Cloud project.
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.Make sure that billing is enabled for your Google Cloud project.
- Enable the Cloud APIs necessary to run a Cloud SQL sample app on a Compute Engine VM instance.
Console
Click theEnable APIs button to enable the API required for this quickstart.
This enables the following API:
- Cloud SQL Admin API
gcloud
Install thegcloud CLI which provides command-line access to your Google Cloud resources. Thegcloud 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:gcloudservicesenablesqladmin.googleapis.com
This command enables the following API:
- Cloud SQL Admin API
Set up Cloud SQL
Create a Cloud SQL instance
Public IP
Console
Create an instance with a public IP address
In the Google Cloud console, go to theCloud SQL Instances page.
- ClickCreate instance.
- ClickPostgreSQL.
- In theInstance ID field, enter
quickstart-instance
. - In thePassword field, enter a password for the postgres user. Save this password for future use.
- In theChoose region and zonal availability section, selectSingle zone.
- Expand theShow Configurations section.
- In theMachine Type drop-down list, selectLightweight.
- ClickCreate Instance and then wait until the instance initializes and starts.
gcloud
Create an instance with a public IP address
Before running thegcloud sql instances create
command as follows, replaceDB_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 thegcloud 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 ofregions.
Run thegcloud sql instances create
command to create a Cloud SQL instance.
gcloudsqlinstancescreatequickstart-instance--database-version=POSTGRES_13--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
In the Google Cloud console, go to theVPC networks page.
- Select the
default
VPC network. - Select thePrivate service connection tab.
- Select theAllocated IP ranges for services tab.
- ClickAllocate IP range.
- For theName of the allocated range, specify
google-managed-services-default
. - Select theAutomatic option for IP range and specify the prefix length as
16
. - ClickAllocate to create the allocated range.
- Select thePrivate connections to services tab for the
default
VPC network. - ClickCreate connection to create a private connection between your network and a service producer.
- For theAssigned allocation, select
google-managed-services-default
. - ClickConnect to create the connection.
Create an instance with private IP address and SSL enabled
In the Google Cloud console, go to theCloud SQL Instances page.
- ClickCreate instance.
- ClickPostgreSQL.
- Enter
quickstart-instance
forInstance ID. - Enter a password for the postgres user. Save this password for future use.
- Click theSingle zone option forChoose region and zonal availability.
- Click and expandShow configuration options.
- ForMachine Type, selectLightweight.
- InConnections, selectPrivate IP.
- Selectdefault in theNetwork drop-down menu.
- Clear thePublic IP checkbox to create an instance only with a private IP.
- ClickCreate instance and then wait for the instance to initialize and start.
- ClickConnections.
- In theSecurity section, selectAllow only SSL connections to enable SSL connections.
- In theAllow only SSL connections dialog, clickSave 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
Run the
gcloud compute addresses create
command to allocate an IP address range.gcloudcomputeaddressescreategoogle-managed-services-default--global--purpose=VPC_PEERING--prefix-length=16--description="peering range for Google"--network=default
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.gcloudservicesvpc-peeringsconnect--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
Before running the command as follows, replaceDB_ROOT_PASSWORD with the password of your database user.
- --database_version: The database engine type and version. If left unspecified, the API default is used. See thegcloud 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 ofregions. Run the
gcloud sql instances patch
command to enableonly allow SSL connections for the instance.
Optionally, modify the values for the following parameters:
Run thegcloud sql instances create
command to create a Cloud SQL instance with a Private IP address.
gcloudbetasqlinstancescreatequickstart-instance--database-version=POSTGRES_13--cpu=1--memory=4GB--region=us-central--root-password=DB_ROOT_PASSWORD--no-assign-ip--network=default
gcloudsqlinstancespatchquickstart-instance--require-ssl
Create a database
Console
In the Google Cloud console, go to theCloud SQL Instances page.
- Select
quickstart-instance
. - Open theDatabases tab.
- ClickCreate database.
- In theNew database dialog box, enter
quickstart_db
as the name of the database. - ClickCreate.
gcloud
Run thegcloud sql databases create
command to create a database.
gcloudsqldatabasescreatequickstart_db--instance=quickstart-instance
Create a user
Console
In the Google Cloud console, go to theCloud SQL Instances page.
- To open theOverview page of an instance, click the instance name.
- SelectUsers from the SQL navigation menu.
- ClickAdd user account.
- On theAdd a user account to instanceinstance_name page, add the following information:
- In theUsername field, enter
quickstart-user
- In thePassword field, specify a password for your database user. Make a note of this for use in a later step of this quickstart.
- In theUsername field, enter
- ClickAdd.
gcloud
Before running the following command, make the following replacements:
- PASSWORD with a password for your database user. Make a note of this for use in a later step of this quickstart.
Run thegcloud sql users create
command to create the user.
gcloudsqluserscreatequickstart-user--instance=quickstart-instance--password=PASSWORD
User name length limits are the same for Cloud SQL as foron-premises PostgreSQL.
Configure a Compute Engine service account
Console
Create a service account
- In the Google Cloud console, go to theCreate service account page.
- Select a Google Cloud project.
- Enter a
quickstart-service-account
as the service account name. - Optional: Enter a description of the service account.
- ClickCreate and continue and continue to the next step.
- Choose theCloud SQL Client role to grant to the service account on the project.
- ClickAdd another role and choose theStorage Object Viewer role to grant to the service account on the project.
- ClickContinue.
- ClickDone to finish creating the service account.
gcloud
Create a service account
- To create the service account, run the
gcloud iam service-accounts create
command: DESCRIPTION
: an optional description of the service account- To grant your service account theCloud SQL Client role and theStorage Object Viewer role on your project, run the
gcloud projects add-iam-policy-binding
command. ReplacePROJECT_ID with your Google Cloud project ID:gcloudprojectsadd-iam-policy-bindingPROJECT_ID--member="serviceAccount:quickstart-service-account@PROJECT_ID.iam.gserviceaccount.com"--role="roles/cloudsql.client"--role="roles/storage.objectViewer"
gcloudiamservice-accountscreatequickstart-service-account--description="DESCRIPTION"--display-name="quickstart-service-account"
Replace the following value:
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
Console
In the Google Cloud console, go to theVM instances page.
- Click theCreate instance button.
- For VM instanceName enter
quickstart-vm-instance
. - ForService accounts select
quickstart-service-account
. - ForFirewall select theAllow HTTP traffic option.
- ClickCreate to create the VM instance.
gcloud
Before running the following command, replaceYOUR_PROJECT_ID with your project ID.
Run the followinggcloud compute instances create
command.
gcloudcomputeinstancescreatequickstart-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
Create a Windows VM Instance
Console
In the Google Cloud console, go to theVM instances page.
- Click theCreate instance button.
- For VM instanceName enter
quickstart-vm-instance
. - ForBoot disc click theChange button.
- ForOperating system selectWindows Server.
- ForVersion selectWindows Server 2022 Datacenter.
- Click theSelect button.
- ForService accounts select
quickstart-service-account
. - ForFirewall select theAllow HTTP traffic option.
- ClickCreate to create the VM instance.
- After 2-3 minutes once the VM instance has started, click theSet Windows Password button on the VM instance details page.
- 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.
gcloud
Before running the following command, replaceYOUR_PROJECT_ID with your project ID.
Run the followinggcloud compute instances create
command in a Terminal Window.
gcloudcomputeinstancescreatequickstart-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.
gcloudcomputereset-windows-passwordquickstart-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
Console
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, seeSSH connections to Linux VMs
gcloud
Use thegcloud compute ssh
command to connect to a Linux VM instance. ReplaceYOUR_PROJECT_ID with your project ID:
gcloudcomputessh--project=YOUR_PROJECT_ID--zone=us-central1-aquickstart-vm-instance
Access Windows VM Instance
Chrome RDP plugin
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 theGoogle Cloud console. After you install the plugin, connect to any Windows Server instance by using theRDP button in theGoogle Cloud console.
To connect using the Chrome RDP plugin, do the following:
- Install theChrome RDP for Google Cloud extension.
- In Google Cloud console, go to theVM instances page and find the Windows instance you want to connect to.
- Click theRDP button for the instance you want to connect to. The Chrome RDP extension opens.
- Since your VM instance does not have a domain configured, you can leave theDomain field blank
- Enter your username, and password, and clickOK to connect.
- If prompted, pressContinue to accept the certificate.

Other
SeeConnect 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
Go
Complete the following steps to set up the Compute Engine VM instance's development environment to run the Go sample app.
- Go to thesetup guide for a Go development environment.
- Complete the instructions in theInstall Go section.
Java
Complete the following steps to set up the Compute Engine VM instance's development environment to run the Java sample app.
- Go to thesetup guide for a Java development environment.
- Complete the instructions in theInstall a JDK (Java Development Kit) section.
- Complete the instructions in theInstall a build automation tool to set up Apache Maven.
Node.js
Complete the following steps to set up the Compute Engine VM instance's development environment to run the Node.js sample app.
- Go tosetup guide for a Node.js development environment.
- Complete the instructions in theInstalling Node.js and npm section.
Python
Complete the following steps to set up the Compute Engine VM instance's development environment to run the Python sample app.
- Go tosetup guide for a Python development environment.
- Complete the instructions in theInstalling Python section.
Setup Windows VM Instance development environment
Go
Complete the following steps to set up the Compute Engine VM instance's development environment to run the Go sample app.
- Go to thesetup guide for a Go development environment.
- Complete the instructions in theInstall Go section.
Java
Complete the following steps to set up the Compute Engine VM instance's development environment to run the Java sample app.
- Go to thesetup guide for a Java development environment.
- Complete the instructions in theInstall a JDK (Java Development Kit) section.
- Complete the instructions in theInstall a build automation tool section to set up Apache Maven.
Node.js
Complete the following steps to set up the Compute Engine VM instance's development environment to run the Node.js sample app.
- Go tosetup guide for a Node.js development environment.
- Complete the instructions in theInstalling Node.js and npm section.
Python
Complete the following steps to set up the Compute Engine VM instance's development environment to run the Python sample app.
- Install Python and the PIP package manager for Python.
- Go tosetup guide for a Python development environment.
- Complete the instructions in theInstalling Python section.
- The instructions above will have you visit thePython Releases for Windows download page. From that page click theLatest Python 3 Release link.
- On the Python 3.X.X page, click theWindows Installer 64-bit link to download the installer file to your Windows Compute Engine VM instance.
- 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 selectRun as administrator.
- In theInstall Python dialog that appears, select the option toAdd Python 3.X.X to PATH and click→ Install Now.
- Use PIP to install virtualenv.
- Open Powershell on the Windows Compute Engine VM instance and run the following
pip install
command.
pipinstallvirtualenv
Install Git
InstallGit, 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 officialGit installation documentation for Linux.
- Run the suggestedDebian/Ubuntu
install git
command using thesudo
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
Compute Engine Windows VM instance
On your Compute Engine Windows VM instance, follow the officialGit installation documentation for Windows to download the64-bit Standalone Installer and run it to install Git.
Clone sample app
Clone a sample app to your Compute Engine VM instance using thegit clone
command.
Go
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.
- Clone the sample app.
gitclonehttps://github.com/GoogleCloudPlatform/golang-samples
- Change directory to the directory containing the sample app.
cdgolang-samples/cloudsql/postgres/database-sql
Java
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.
- Clone the sample app.
gitclonehttps://github.com/GoogleCloudPlatform/java-docs-samples
- Change directory to the directory containing the sample app.
cdjava-docs-samples/cloud-sql/postgres/servlet
Node.js
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.
- Clone the sample app.
gitclonehttps://github.com/GoogleCloudPlatform/nodejs-docs-samples
- Change directory to the directory containing the sample app.
cdnodejs-docs-samples/cloud-sql/postgres/knex
Python
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.
- Clone the sample app.
gitclonehttps://github.com/GoogleCloudPlatform/python-docs-samples
- Change directory to the directory containing the sample app.
cdpython-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
Go
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'sConnection name that appears on theCloud SQL instances page in the Google Cloud console.
- YOUR_DB_PASSWORD with the password of the
quickstart-user
that you created in the previousCreate a user quickstart step.
exportINSTANCE_CONNECTION_NAME='INSTANCE_CONNECTION_NAME'exportDB_PORT='5432'exportDB_NAME='quickstart_db'exportDB_USER='quickstart-user'exportDB_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.
- Get the dependencies required to run to sample app.
goget./...
- Run the sample app.
goruncmd/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.
Note: This quickstart step requires thegcloud CLI, which provides command-line access to your Google Cloud resources usinggcloud CLI
commands. If you don't have thegcloud CLI installed on your local computer, theninstall thegcloud CLI to proceed.- 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: - 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
gcloudcomputeinstancesdescribequickstart-vm-instance--format="value(networkInterfaces[0].accessConfigs[].natIP)"
To stop the sample app, pressControl+C in the Compute Engine VM instance terminal where you started the sample app.
Java
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'sConnection name that appears on theCloud SQL instances page in the Google Cloud console.
- YOUR_DB_PASSWORD with the password of the
quickstart-user
that you created in the previousCreate a user quickstart step.
exportINSTANCE_CONNECTION_NAME='INSTANCE_CONNECTION_NAME'exportDB_PORT='5432'exportDB_NAME='quickstart_db'exportDB_USER='quickstart-user'exportDB_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.
mvnjetty: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.
Note: This quickstart step requires thegcloud CLI, which provides command-line access to your Google Cloud resources usinggcloud CLI
commands. If you don't have thegcloud CLI installed on your local computer, theninstall thegcloud CLI to proceed.- 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: - 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
gcloudcomputeinstancesdescribequickstart-vm-instance--format="value(networkInterfaces[0].accessConfigs[].natIP)"
To stop the sample app, pressControl+C in the Compute Engine VM instance terminal where you started the sample app.
Node.js
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 previousCreate a user quickstart step.
exportINSTANCE_HOST='127.0.0.1'exportDB_PORT='5432'exportDB_NAME='quickstart_db'exportDB_USER='quickstart-user'exportDB_PASS='YOUR_DB_PASSWORD'
- YOUR_DB_PASSWORD with the password of the
- Download the Cloud SQL Auth Proxy.
curl-ocloud-sql-proxy\https://storage.googleapis.com/cloud-sql-connectors/cloud-sql-proxy/v2.0.0/cloud-sql-proxy.linux.amd64
- Set permissions to make the Cloud SQL Auth Proxy file executable.
chmod+xcloud-sql-proxy
- Run the Cloud SQL Auth Proxy as a background process. ReplaceINSTANCE_CONNECTION_NAME with your instance'sConnection name that appears on theCloud SQL instances page in the Google Cloud console.
./cloud-sql-proxyINSTANCE_CONNECTION_NAME&
fg
command in your Terminal window where you started the sample app. This should bring the running Cloud SQL Auth Proxy job to the terminal foreground. Then press theCtrl +C keys to stop the proxy job.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.
- Install the Node.js packages necessary to run the app locally.
npminstall
- Run the sample app.
npmstart
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.
Note: This quickstart step requires thegcloud CLI, which provides command-line access to your Google Cloud resources usinggcloud CLI
commands. If you don't have thegcloud CLI installed on your local computer, theninstall thegcloud CLI to proceed.- 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: - 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
gcloudcomputeinstancesdescribequickstart-vm-instance--format="value(networkInterfaces[0].accessConfigs[].natIP)"
To stop the sample app, pressControl+C in the Compute Engine VM instance terminal where you started the sample app.
Python
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'sConnection name that appears on theCloud SQL instances page in the Google Cloud console.
- YOUR_DB_PASSWORD with the password of the
quickstart-user
that you created in the previousCreate a user quickstart step.
exportINSTANCE_CONNECTION_NAME='INSTANCE_CONNECTION_NAME'exportDB_PORT='5432'exportDB_NAME='quickstart_db'exportDB_USER='quickstart-user'exportDB_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.
- Initialize a virtual environment and install the requirements to run to sample app.
python3-mvenvenvsourceenv/bin/activatepipinstall-rrequirements.txt
- Run the sample app.
pythonapp.py
On your local computer, set upport 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.
Note: This quickstart step requires thegcloud CLI, which provides command-line access to your Google Cloud resources usinggcloud CLI
commands. If you don't have thegcloud CLI installed on your local computer, theninstall thegcloud CLI to proceed.- 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, replaceYOUR_PROJECT_ID with your project ID. - 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..
gcloudcomputesshquickstart-vm-instance--project=YOUR_PROJECT_ID--zone=us-central1-a--ssh-flag='-L 8000:127.0.0.1:8080'
To stop the sample app, pressControl+C in the Compute Engine VM instance terminal where you started the sample app.
Public IP Cloud SQL Instance and Windows based Compute Engine VM
Go
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'sConnection name that appears on theCloud SQL instances page in the Google Cloud console.
- YOUR_DB_PASSWORD with the password of the
quickstart-user
that you created in the previousCreate 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.
- Get the dependencies required to run to sample app.
goget./...
- Run the sample app.
goruncmd\app\main.go
- View the running sample app. Open a browser on the Compute Engine VM instance and go to
http://127.0.0.1:8080
.
To stop the sample app, pressControl+C in the Compute Engine VM instance Powershell window where you started the sample app.
Java
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'sConnection name that appears on theCloud SQL instances page in the Google Cloud console.
- YOUR_DB_PASSWORD with the password of the
quickstart-user
that you created in the previousCreate 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 command to get the Java sample app's dependencies on to your Compute Engine VM instance and run the sample app.
mvnjetty:run
- View the running sample app. Open a browser on the Compute Engine VM instance and go to
http://127.0.0.1:8080
.
To stop the sample app, pressControl+C in the Compute Engine VM instance Powershell window where you started the sample app.
Node.js
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 previousCreate 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"
- YOUR_DB_PASSWORD with the password of the
- Download the Cloud SQL Auth Proxy.
wgethttps://storage.googleapis.com/cloud-sql-connectors/cloud-sql-proxy/v2.0.0/cloud-sql-proxy.x64.exe`-Ocloud-sql-proxy.exe
- Run the Cloud SQL Auth Proxy as a background process. ReplaceINSTANCE_CONNECTION_NAME with your instance'sConnection name that appears on theCloud SQL instances page in the Google Cloud console.
Start-Process-filepath".\cloud-sql-proxy.exe"-ArgumentList`"INSTANCE_CONNECTION_NAME"
Start-Process
command. Then close the window to stop the proxy job.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.
- Install the Node.js packages necessary to run the app locally.
npminstall
- Run the sample app.
npmstart
- View the running sample app. Open a browser on the Compute Engine VM instance and go to
http://127.0.0.1:8080
.
To stop the sample app, pressControl+C in the Compute Engine VM instance Powershell window where you started the sample app.
Python
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'sConnection name that appears on theCloud SQL instances page in the Google Cloud console.
- YOUR_DB_PASSWORD with the password of the
quickstart-user
that you created in the previousCreate 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.
- Initialize a virtual environment and install the requirements to run to sample app.
virtualenv--pythonpython3env.\env\Scripts\activatepipinstall-rrequirements.txt
- Run the sample app.
pythonapp.py
- View the running sample app. Open a browser on the Compute Engine VM instance and go to
http://127.0.0.1:8080
.
To stop the sample app, pressControl+C in the Compute Engine VM instance Powershell window where you started the sample app.
Private IP Cloud SQL Instance and Linux based Compute Engine VM
Create and download SSL server and client certificates on to your local computer
In the Google Cloud console, go to theCloud SQL Instances page.
- Click the
quickstart-instance
to see itsOverview page - Click theConnections tab.
- Under theSecurity section, clickCreate client certificate.
- In theCreate a client certificate dialog, enter
quickstart-key
as the name and clickCreate. In theNew SSL certificate created dialog, click each download link to download the certificates. Then, clickClose.
Important: Store this private key securely. If you lose it, you mustcreate a new client certificate.
Upload SSL certificates to Cloud Storage bucket
Console
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.
- Create a Cloud Storage bucket.
- ForName of your bucket, enter the following name. ReplaceYOUR_PROJECT_ID with your project ID:
YOUR_PROJECT_ID-quickstart-certs
- Click theCreate button to create the bucket.
- Click theUpload Files button to upload files to the newly created Cloud Storage bucket.
- Select the following files to be uploaded from your local computer to Cloud Storage:
server-ca.pem
client-cert.pem
client-key.pem
gcloud
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. Thegcloud CLI will be used to upload the files.
- Run the following
gcloud storage buckets create
command to make a new Cloud Storage bucket: - 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:
gcloudstoragebucketscreategs://YOUR_PROJECT_ID-quickstart-certs--location=us-central1
gcloudstoragecpserver-ca.pemgs://YOUR_PROJECT_ID-quickstart-certs/gcloudstoragecpclient-cert.pemgs://YOUR_PROJECT_ID-quickstart-certs/gcloudstoragecpclient-key.pemgs://YOUR_PROJECT_ID-quickstart-certs/
Go
Download SSL certificates using the gcloud CLI on the Compute Engine VM instance
On the Compute Engine VM instance in a terminal open to thegolang-samples/cloudsql/postgres/database-sql
directory, run the followinggcloud storage cp
commands to download the SSL certificates from Cloud Storage to thecerts
directory.
gcloudstoragecpgs://YOUR_PROJECT_ID-quickstart-certs/server-ca.pemcerts/.gcloudstoragecpgs://YOUR_PROJECT_ID-quickstart-certs/client-cert.pemcerts/.gcloudstoragecpgs://YOUR_PROJECT_ID-quickstart-certs/client-key.pemcerts/.
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 thePrivate IP address of your instance displayed on theCloud SQL instances page in the Google Cloud console. Note: Connections to a Cloud SQL instance using a private IP address are automatically authorized for RFC 1918 address ranges. For Non-RFC 1918 address ranges, seeLearn about using private IP.
- YOUR_DB_PASSWORD with the password of the
quickstart-user
that you created in the previousCreate a user quickstart step.
exportINSTANCE_HOST='INSTANCE_HOST'exportDB_PORT='5432'exportDB_NAME='quickstart_db'exportDB_USER='quickstart-user'exportDB_PASS='YOUR_DB_PASSWORD'exportDB_ROOT_CERT='certs/server-ca.pem'exportDB_CERT='certs/client-cert.pem'exportDB_KEY='certs/client-key.pem'exportPRIVATE_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.
- Get the dependencies required to run to sample app.
goget./...
- Run the sample app.
goruncmd/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.
Note: This quickstart step requires thegcloud CLI, which provides command-line access to your Google Cloud resources usinggcloud CLI
commands. If you don't have thegcloud CLI installed on your local computer, theninstall thegcloud CLI to proceed.- 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: - 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
gcloudcomputeinstancesdescribequickstart-vm-instance--format="value(networkInterfaces[0].accessConfigs[].natIP)"
To stop the sample app, pressControl+C in the Compute Engine VM instance terminal where you started the sample app.
Java
Download SSL certificates using the gcloud CLI on the Compute Engine VM instance
On the Compute Engine VM instance in a terminal open to thejava-docs-samples/cloud-sql/postgres/servlet
directory, run the followinggcloud storage cp
commands to download the SSL certificates from Cloud Storage to the current directory.
gcloudstoragecpgs://YOUR_PROJECT_ID-quickstart-certs/server-ca.pem.gcloudstoragecpgs://YOUR_PROJECT_ID-quickstart-certs/client-cert.pem.gcloudstoragecpgs://YOUR_PROJECT_ID-quickstart-certs/client-key.pem.
Configure the SSL certificates for use in Java on the Compute Engine VM instance
- 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, replaceSSL_CLIENT_KEY_PASSWD with your own custom keystore password to be used to create the Java client keystore.
opensslpkcs12-export-inclient-cert.pem-inkeyclient-key.pem\-name"postgresclient"-passoutpass:SSL_CLIENT_KEY_PASSWD\-outclient-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 thePrivate IP address of your instance displayed on theCloud SQL instances page in the Google Cloud console. Note: Connections to a Cloud SQL instance using a private IP address are automatically authorized for RFC 1918 address ranges. For Non-RFC 1918 address ranges, seeLearn about using private IP.
- 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 previousCreate a user quickstart step.
exportINSTANCE_HOST='INSTANCE_HOST'exportDB_PORT='5432'exportDB_NAME='quickstart_db'exportDB_USER='quickstart-user'exportDB_PASS='YOUR_DB_PASSWORD'exportSSL_CLIENT_KEY_PATH='client-keystore.p12'exportSSL_CLIENT_KEY_PASSWD='SSL_CLIENT_KEY_PASSWD'exportSSL_SERVER_CA_PATH='server-ca.pem'exportPRIVATE_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.
mvnjetty: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.
Note: This quickstart step requires thegcloud CLI, which provides command-line access to your Google Cloud resources usinggcloud CLI
commands. If you don't have thegcloud CLI installed on your local computer, theninstall thegcloud CLI to proceed.- 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: - 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
gcloudcomputeinstancesdescribequickstart-vm-instance--format="value(networkInterfaces[0].accessConfigs[].natIP)"
To stop the sample app, pressControl+C in the Compute Engine VM instance terminal where you started the sample app.
Node.js
Download SSL certificates using the gcloud CLI on the Compute Engine VM instance
On the Compute Engine VM instance in a terminal open to thenodejs-docs-samples/cloud-sql/postgres/knex
directory, run the followinggcloud storage cp
commands to download the SSL certificates from Cloud Storage to thecerts
directory.
gcloudstoragecpgs://YOUR_PROJECT_ID-quickstart-certs/server-ca.pemcerts/.gcloudstoragecpgs://YOUR_PROJECT_ID-quickstart-certs/client-cert.pemcerts/.gcloudstoragecpgs://YOUR_PROJECT_ID-quickstart-certs/client-key.pemcerts/.
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 thePrivate IP address of your instance displayed on theCloud SQL instances page in the Google Cloud console. Note: Connections to a Cloud SQL instance using a private IP address are automatically authorized for RFC 1918 address ranges. For Non-RFC 1918 address ranges, seeLearn about using private IP.
- YOUR_DB_PASSWORD with the password of the
quickstart-user
that you created in the previousCreate a user quickstart step.
exportINSTANCE_HOST='INSTANCE_HOST'exportDB_PORT='5432'exportDB_NAME='quickstart_db'exportDB_USER='quickstart-user'exportDB_PASS='YOUR_DB_PASSWORD'exportDB_ROOT_CERT='certs/server-ca.pem'exportDB_CERT='certs/client-cert.pem'exportDB_KEY='certs/client-key.pem'exportPRIVATE_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.
- Install the Node.js packages necessary to run the app locally.
npminstall
- Run the sample app.
npmstart
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.
Note: This quickstart step requires thegcloud CLI, which provides command-line access to your Google Cloud resources usinggcloud CLI
commands. If you don't have thegcloud CLI installed on your local computer, theninstall thegcloud CLI to proceed.- 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: - 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
gcloudcomputeinstancesdescribequickstart-vm-instance--format="value(networkInterfaces[0].accessConfigs[].natIP)"
To stop the sample app, pressControl+C in the Compute Engine VM instance terminal where you started the sample app.
Python
Download SSL certificates using the gcloud CLI on the Compute Engine VM instance
On the Compute Engine VM instance in a terminal open to thepython-docs-samples/cloud-sql/postgres/sqlalchemy
directory, run the followinggcloud storage cp
commands to download the SSL certificates from Cloud Storage to thecerts
directory.
gcloudstoragecpgs://YOUR_PROJECT_ID-quickstart-certs/server-ca.pemcerts/.gcloudstoragecpgs://YOUR_PROJECT_ID-quickstart-certs/client-cert.pemcerts/.gcloudstoragecpgs://YOUR_PROJECT_ID-quickstart-certs/client-key.pemcerts/.
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 thePrivate IP address of your instance displayed on theCloud SQL instances page in the Google Cloud console. Note: Connections to a Cloud SQL instance using a private IP address are automatically authorized for RFC 1918 address ranges. For Non-RFC 1918 address ranges, seeLearn about using private IP.
- YOUR_DB_PASSWORD with the password of the
quickstart-user
that you created in the previousCreate a user quickstart step.
exportINSTANCE_HOST='INSTANCE_HOST'exportDB_PORT='5432'exportDB_NAME='quickstart_db'exportDB_USER='quickstart-user'exportDB_PASS='YOUR_DB_PASSWORD'exportDB_ROOT_CERT='certs/server-ca.pem'exportDB_CERT='certs/client-cert.pem'exportDB_KEY='certs/client-key.pem'exportPRIVATE_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.
- Initialize a virtual environment and install the requirements to run to sample app.
python3-mvenvenvsourceenv/bin/activatepipinstall-rrequirements.txt
- Run the sample app.
pythonapp.py
On your local computer, set upport 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.
Note: This quickstart step requires thegcloud CLI, which provides command-line access to your Google Cloud resources usinggcloud CLI
commands. If you don't have thegcloud CLI installed on your local computer, theninstall thegcloud CLI to proceed.- 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, replaceYOUR_PROJECT_ID with your project ID. - 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.
gcloudcomputesshquickstart-vm-instance--project=YOUR_PROJECT_ID--zone=us-central1-a--ssh-flag='-L 8000:127.0.0.1:8080'
To stop the sample app, pressControl+C in the Compute Engine VM instance terminal where you started the sample app.
Private IP Cloud SQL Instance and Windows based Compute Engine VM
Create and download SSL server and client certificates on to your local computer
In the Google Cloud console, go to theCloud SQL Instances page.
- Click the
quickstart-instance
to see itsOverview page - Click theConnections tab.
- Under theSecurity section, clickCreate client certificate.
- In theCreate a client certificate dialog, enter
quickstart-key
as the name and clickCreate. In theNew SSL certificate created dialog, click each download link to download the certificates. Then, clickClose.
Important: Store this private key securely. If you lose it, you mustcreate a new client certificate.
Upload SSL certificates to Cloud Storage bucket
Console
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.
- Create a Cloud Storage bucket.
- ForName of your bucket, enter the following name. ReplaceYOUR_PROJECT_ID with your project ID:
YOUR_PROJECT_ID-quickstart-certs
- Click theCreate button to create the bucket.
- Click theUpload Files button to upload files to the newly created Cloud Storage bucket.
- Select the following files to be uploaded from your local computer to Cloud Storage:
server-ca.pem
client-cert.pem
client-key.pem
gcloud
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. Thegcloud CLI will be used to upload the files.
- Run the following
gcloud storage buckets create
command to make a new Cloud Storage bucket: - 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:
gcloudstoragebucketscreategs://YOUR_PROJECT_ID-quickstart-certs--location=us-central1
gcloudstoragecpserver-ca.pemgs://YOUR_PROJECT_ID-quickstart-certs/gcloudstoragecpclient-cert.pemgs://YOUR_PROJECT_ID-quickstart-certs/gcloudstoragecpclient-key.pemgs://YOUR_PROJECT_ID-quickstart-certs/
Go
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 thegolang-samples/cloudsql/postgres/database-sql
directory, run the followinggcloud storage cp
commands to download the SSL certificates from Cloud Storage to thecerts
directory.
gcloudstoragecpgs://YOUR_PROJECT_ID-quickstart-certs/server-ca.pemcerts/.gcloudstoragecpgs://YOUR_PROJECT_ID-quickstart-certs/client-cert.pemcerts/.gcloudstoragecpgs://YOUR_PROJECT_ID-quickstart-certs/client-key.pemcerts/.
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 thePrivate IP address of your instance displayed on theCloud SQL instances page in the Google Cloud console. Note: Connections to a Cloud SQL instance using a private IP address are automatically authorized for RFC 1918 address ranges. For Non-RFC 1918 address ranges, seeLearn about using private IP.
- YOUR_DB_PASSWORD with the password of the
quickstart-user
that you created in the previousCreate 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.
- Get the dependencies required to run to sample app.
goget./...
- Run the sample app.
goruncmd\app\main.go
- View the running sample app. Open a browser on the Compute Engine VM instance and go to
http://127.0.0.1:8080
.
To stop the sample app, pressControl+C in the Compute Engine VM instance Powershell window where you started the sample app.
Java
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 thejava-docs-samples/cloud-sql/postgres/servlet
directory, run the followinggcloud storage cp
commands to download the SSL certificates from Cloud Storage to the current directory.
gcloudstoragecpgs://YOUR_PROJECT_ID-quickstart-certs/server-ca.pem.gcloudstoragecpgs://YOUR_PROJECT_ID-quickstart-certs/client-cert.pem.gcloudstoragecpgs://YOUR_PROJECT_ID-quickstart-certs/client-key.pem.
Configure the SSL certificates for use in Java on the Compute Engine VM instance
- 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, replaceSSL_CLIENT_KEY_PASSWD with your own custom keystore password to be used to create the Java client keystore.
opensslpkcs12-export-inclient-cert.pem-inkeyclient-key.pem`-name"postgresclient"-passoutpass:SSL_CLIENT_KEY_PASSWD`-outclient-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 thePrivate IP address of your instance displayed on theCloud SQL instances page in the Google Cloud console. Note: Connections to a Cloud SQL instance using a private IP address are automatically authorized for RFC 1918 address ranges. For Non-RFC 1918 address ranges, seeLearn about using private IP.
- YOUR_DB_PASSWORD with the password of the
quickstart-user
that you created in the previousCreate a user quickstart step. - SSL_CLIENT_KEY_PASSWD with the password you specified forSSL_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"
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.
mvnjetty:run
- View the running sample app. Open a browser on the Compute Engine VM instance and go to
http://127.0.0.1:8080
.
To stop the sample app, pressControl+C in the Compute Engine VM instance Powershell window where you started the sample app.
Node.js
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 thenodejs-docs-samples/cloud-sql/postgres/knex
directory, run the followinggcloud storage cp
commands to download the SSL certificates from Cloud Storage to thecerts
directory.
gcloudstoragecpgs://YOUR_PROJECT_ID-quickstart-certs/server-ca.pemcerts/.gcloudstoragecpgs://YOUR_PROJECT_ID-quickstart-certs/client-cert.pemcerts/.gcloudstoragecpgs://YOUR_PROJECT_ID-quickstart-certs/client-key.pemcerts/.
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 thePrivate IP address of your instance displayed on theCloud SQL instances page in the Google Cloud console. Note: Connections to a Cloud SQL instance using a private IP address are automatically authorized for RFC 1918 address ranges. For Non-RFC 1918 address ranges, seeLearn about using private IP.
- YOUR_DB_PASSWORD with the password of the
quickstart-user
that you created in the previousCreate 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.
- Install the Node.js packages necessary to run the app locally.
npminstall
- Run the sample app.
npmstart
- View the running sample app. Open a browser on the Compute Engine VM instance and go to
http://127.0.0.1:8080
.
To stop the sample app, pressControl+C in the Compute Engine VM instance Powershell window where you started the sample app.
Python
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 thepython-docs-samples/cloud-sql/postgres/sqlalchemy
directory, run the followinggcloud storage cp
commands to download the SSL certificates from Cloud Storage to thecerts
directory.
gcloudstoragecpgs://YOUR_PROJECT_ID-quickstart-certs/server-ca.pemcerts/.gcloudstoragecpgs://YOUR_PROJECT_ID-quickstart-certs/client-cert.pemcerts/.gcloudstoragecpgs://YOUR_PROJECT_ID-quickstart-certs/client-key.pemcerts/.
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 thePrivate IP address of your instance displayed on theCloud SQL instances page in the Google Cloud console. Note: Connections to a Cloud SQL instance using a private IP address are automatically authorized for RFC 1918 address ranges. For Non-RFC 1918 address ranges, seeLearn about using private IP.
- YOUR_DB_PASSWORD with the password of the
quickstart-user
that you created in the previousCreate 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.
- Initialize a virtual environment and install the requirements to run to sample app.
virtualenv--pythonpython3env.\env\Scripts\activatepipinstall-rrequirements.txt
- Run the sample app.
pythonapp.py
- View the running sample app. Open a browser on the Compute Engine VM instance and go to
http://127.0.0.1:8080
.
To stop the sample app, pressControl+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
In the Google Cloud console, go to theCloud SQL Instances page.
- Select the
quickstart-instance
instance to open theInstance detailspage. - In the icon bar at the top of the page, clickDelete.
- In theDelete instance dialog box, type
quickstart-instance
, and thenclickDelete to delete the instance.
Delete Compute Engine VM instance
In the Google Cloud console, go to theVM instances page.
- Select the
quickstart-vm-instance
instance to open theInstance detailspage. - In the icon bar at the top of the page, clickDelete.
- ClickDelete to delete the instance.
Optional cleanup steps
If you're not using theCloud SQL client role that you assigned to theCompute Engine default
service account, you can remove it.
In the Google Cloud console, go to theIAM page.
- Click the edit icon (which looks like a pencil) for the IAM account namedCompute Engine default service account.
- Delete theCloud SQL client role.
- ClickSave.
If you're not using the API that was enabled as part of this quickstart, youcan disable it.
- API that was enabled within this quickstart:
- Cloud SQL Admin API
In the Google Cloud console, go to theAPIs page.
Select any API that you would like to disable and then click theDisable API button.
What's next
Based on your needs, you can learn more aboutcreating Cloud SQL instances.You also can learn about creatingPostgreSQL users anddatabases for your Cloud SQL instance.
For more information about pricing, seeCloud SQL for PostgreSQL pricing.
Learn more about:
- Configuring your Cloud SQL instance with apublic IP address.
- Configuring your Cloud SQL instance with aprivate IP address.
Additionally, you can learn about connecting to a Cloud SQL instance fromother Google Cloud applications:
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-07-14 UTC.