All Cloud SQL for PostgreSQL code samples
This page contains code samples for Cloud SQL for PostgreSQL. To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.
Create a connection using the Cloud SQL Go Connector
Open a connection to Cloud SQL for Postgres by using the Cloud SQL Go Connector.
- Cloud SQL for PostgreSQL
- Cloud SQL
- Google Cloud
- Go
Create a socket connection by using Java
Open a Unix socket connection to Cloud SQL for PostgreSQL by using Java.
- Cloud SQL
- Google Cloud
- Cloud SQL for PostgreSQL
- Java
PostgreSQL knex connection
Insert data by using the Node.js knex module to manage opening and closing a connection to Cloud SQL for PostgreSQL.
- Cloud SQL for PostgreSQL
- Google Cloud
- Cloud SQL
- JavaScript
- Node JS
Create a TCP connection by using Python
Open a TCP connection to Cloud SQL for PostgreSQL by using Python.
- Cloud SQL
- Cloud SQL for PostgreSQL
- Google Cloud
- Python
Set automated backup retention for an instance
This sample specifies the number of automated backups to retain for an instance.
- Google Cloud
- Cloud SQL
- Cloud SQL for PostgreSQL
- Terraform
Clone a Cloud SQL for Postgres Enterprise Plus instance
This Terraform shows how to create a clone of a Postgres instance
- Cloud SQL for PostgreSQL
- Google Cloud
- Cloud SQL
- Terraform
Set a custom location for backups
This sample sets a custom location for backups to us-central1.
- Cloud SQL for PostgreSQL
- Cloud SQL
- Google Cloud
- Terraform
Clone a Cloud SQL for Postgres instance
This Terraform shows how to clone a Postgres instance
- Cloud SQL for PostgreSQL
- Google Cloud
- Cloud SQL
- Terraform
Set the connection timeout when using HikariCP
Configure a connection timeout when connecting to Cloud SQL for PostgreSQL by using the HikariCP JDBC connection pool library.
- Cloud SQL for PostgreSQL
- Google Cloud
- Cloud SQL
- Java
Set the connection duration when using HikariCP
Configure the maximum number of seconds a connection can persist when connecting to Cloud SQL for PostgreSQL by using the HikariCP JDBC connection. pool library.
- Cloud SQL for PostgreSQL
- Cloud SQL
- Google Cloud
- Java
Create an instance with password policy enabled
This sample creates an instance with password policy enabled.
- Google Cloud
- Cloud SQL
- Cloud SQL for PostgreSQL
- Terraform
Create a connection using the Cloud SQL Python Connector
Open a connection to Cloud SQL for PostgreSQL by using the Cloud SQL Python Connector.
- Cloud SQL
- Cloud SQL for PostgreSQL
- Google Cloud
- Python
Set connection pool and overflow limits when using Node.js
Configure the connection pool size and overflow when connecting to Cloud SQL for PostgreSQL by using the Node.js knex module.
- Cloud SQL for PostgreSQL
- Cloud SQL
- Google Cloud
- JavaScript
- Node JS
Retry a failed connection when using SQLAlchemy
Retry failed connections when connecting to Cloud SQL for PostgreSQL by using SQLAlchemy.
- Cloud SQL
- Cloud SQL for PostgreSQL
- Google Cloud
- Python
Set the connection timeout when using PHP
Configure a connection timeout when connecting to Cloud SQL for PostgreSQL by using the PHP Data Objects (PDO) extension.
- Google Cloud
- Cloud SQL for PostgreSQL
- Cloud SQL
- PHP
Create a socket connection by using PHP
Open a Unix socket connection to Cloud SQL for PostgreSQL by using PHP.
- Google Cloud
- Cloud SQL
- Cloud SQL for PostgreSQL
- PHP
Connect to Cloud SQL for Postgres using Go and IAM permissions
Connect to Cloud SQL for Postgres using Go and IAM permissions to control who/what can connect to your Cloud SQL instances.
- Google Cloud
- Cloud SQL for PostgreSQL
- Cloud SQL
- Go
Configure SSL certificates for TCP connection by using Go
Configure SSL (Secure Sockets Layer) certificates for a TCP connection to Cloud SQL for PostgreSQL by using Go's database/sql package.
- Cloud SQL
- Cloud SQL for PostgreSQL
- Google Cloud
- Go
PostgreSQL ADO.NET connection
Run a SQL INSERT statement to open and close a connection to Cloud SQL for PostgreSQL by using the ADO.NET System.Data.Common package.
- Cloud SQL
- Cloud SQL for PostgreSQL
- Google Cloud
- C#
Implement exponential backoff when using ADO.NET
Retry a failed connection by using exponential backoff when connecting to Cloud SQL for PostgreSQL by using ADO.NET with the Polly .NET library.
- Cloud SQL
- Cloud SQL for PostgreSQL
- Google Cloud
- C#
Configure public IP for a instance
This sample enable public IP and adds an authorized address range for a instance.
- Cloud SQL
- Cloud SQL for PostgreSQL
- Google Cloud
- Terraform
Retry a failed connection when using HikariCP
Retry failed connections when connecting to Cloud SQL for PostgreSQL by using the HikariCP JDBC connection pool library.
- Cloud SQL
- Cloud SQL for PostgreSQL
- Google Cloud
- Java
Set the connection timeout when using ADO.NET
Configure the connection timeout when connecting to Cloud SQL for PostgreSQL by using the ADO.NET Connection object.
- Cloud SQL for PostgreSQL
- Cloud SQL
- Google Cloud
- C#
Set the connection timeout when using SQLAlchemy
Configure a connection timeout when connecting to Cloud SQL for PostgreSQL by using SQLAlchemy.
- Cloud SQL
- Google Cloud
- Cloud SQL for PostgreSQL
- Python
PostgreSQL database sql connection
Run a SQL INSERT statement to open and close a connection to Cloud SQL for PostgreSQL by using Go's database/sql package.
- Cloud SQL
- Google Cloud
- Cloud SQL for PostgreSQL
- Go
PostgreSQL SQLAlchemy opening and closing connections
Run a SQL INSERT statement to open and close a connection to Cloud SQL for PostgreSQL by using SQLAlchemy.
- Cloud SQL for PostgreSQL
- Google Cloud
- Cloud SQL
- Python
Schedule automated backups for an instance
This sample schedules automated backups for an instance starting within 4 hours from 20:55.
- Cloud SQL
- Cloud SQL for PostgreSQL
- Google Cloud
- Terraform
Create a socket connection when using Node.js
Opens a Unix socket connection to Cloud SQL for PostgreSQL by using the Node.js knex module.
- Cloud SQL
- Google Cloud
- Cloud SQL for PostgreSQL
- Node JS
- JavaScript
Configure authorized networks
This sample configures authorized networks to IP address range 192.0.2.0/24.
- Cloud SQL
- Cloud SQL for PostgreSQL
- Google Cloud
- Terraform
Set the connection timeout when using Node.js
Configure a connection timeout when connecting to Cloud SQL for PostgreSQL by using the Node.js knex module.
- Cloud SQL for PostgreSQL
- Google Cloud
- Cloud SQL
- JavaScript
- Node JS
Create a TCP connection by using Node.js
Opens a TCP connection to Cloud SQL for PostgreSQL by using the Node.js knex module.
- Cloud SQL
- Google Cloud
- Cloud SQL for PostgreSQL
- Node JS
- JavaScript
Set connection pool and overflow limits when using ADO.NET
Configure the connection pool size and overflow when connecting to Cloud SQL for PostgreSQL by using the ADO.NET Connection object.
- Cloud SQL
- Google Cloud
- Cloud SQL for PostgreSQL
- C#
PostgreSQL servlet connection
Run a SQL INSERT statement to open and close a connection to Cloud SQL for PostgreSQL by using the HikariCP JDBC connection pool library.
- Cloud SQL
- Google Cloud
- Cloud SQL for PostgreSQL
- Java
Connecting to Cloud SQL for PostgreSQL with Python via a Unix Socket
Establishes a database connection to a Cloud SQL for PostgreSQL instance using a Unix socket. It utilizes the SQLAlchemy library to create a connection engine, configured specifically for the pg8000 driver. The script retrieves necessary connection parameters like database user, password, database name, and the Unix socket path from environment variables. This approach is particularly useful for applications running in the same Google Cloud region as the Cloud SQL instance, offering a secure and efficient way to communicate with the database without needing to configure IP allowlisting for TCP connections.
- Cloud SQL for PostgreSQL
- Google Cloud
- Cloud SQL
- Python
Create a TCP connection by using Go
Open a TCP connection to Cloud SQL for PostgreSQL by using Go's database/sql package.
- Google Cloud
- Cloud SQL
- Cloud SQL for PostgreSQL
- Go
Create a connection using the Cloud SQL Java Connector
Open a connection to Cloud SQL for PostgreSQL by using the Cloud SQL Java Connector.
- Cloud SQL for PostgreSQL
- Cloud SQL
- Google Cloud
- Java
Create a replica Cloud SQL for Postgres instance
This Terraform shows how to create a replica of a Postgres instance
- Cloud SQL for PostgreSQL
- Cloud SQL
- Google Cloud
- Terraform
Create a TCP connection by using ADO.NET
Open a TCP connection to Cloud SQL for PostgreSQL by using an ADO.NET Connection object.
- Cloud SQL for PostgreSQL
- Cloud SQL
- Google Cloud
- C#
Set connection pool and overflow limits when using Go
Configure the connection pool size and overflow when connecting to Cloud SQL for PostgreSQL by using Go's database/sql package.
- Google Cloud
- Cloud SQL
- Cloud SQL for PostgreSQL
- Go
Set connection pool and overflow limits when using HikariCP
Configure the connection pool size and overflow when connecting to Cloud SQL for PostgreSQL by using the HikariCP JDBC connection pool library.
- Cloud SQL
- Google Cloud
- Cloud SQL for PostgreSQL
- Java
PostgreSQL PDO connection
Run a SQL INSERT statement to open and close a connection to Cloud SQL for PostgreSQL by using the PHP Data Objects (PDO) extension.
- Cloud SQL for PostgreSQL
- Google Cloud
- Cloud SQL
- PHP
Create a socket connection when using Go
Open a Unix socket connection to Cloud SQL for PostgreSQL by using Go's database/sql package.
- Cloud SQL
- Google Cloud
- Cloud SQL for PostgreSQL
- Go
Use ActiveRecord to manage connections
Insert data by using ActiveRecord to manage opening and closing a connection to Cloud SQL for PostgreSQL.
- Cloud SQL
- Cloud SQL for PostgreSQL
- Google Cloud
- Ruby
Set connection pool and overflow limits when using SQLAlchemy
Configure the connection pool size and overflow when connecting to Cloud SQL for PostgreSQL by using SQLAlchemy.
- Cloud SQL for PostgreSQL
- Cloud SQL
- Google Cloud
- Python
Create Postgres Private IP
Sample demonstrating how to create the private IP for use with a Postgres Database.
- Google Cloud
- Cloud SQL for PostgreSQL
- Cloud SQL
- Terraform
Upgrade a instance to the target maintenance version
This sample upgrades a instance to the target maintenance version POSTGRES_14_4.R20220710.01_07.
- Cloud SQL
- Google Cloud
- Cloud SQL for PostgreSQL
- Terraform
Create a TCP connection by using Java
Open a TCP connection to Cloud SQL for PostgreSQL by using Java.
- Google Cloud
- Cloud SQL
- Cloud SQL for PostgreSQL
- Java
Create a socket connection when using ADO.NET
Open a Unix socket connection to Cloud SQL for PostgreSQL by using an ADO.NET Connection object.
- Cloud SQL
- Cloud SQL for PostgreSQL
- Google Cloud
- C#
Set the connection timeout when using Go
Configure a connection timeout when connecting to Cloud SQL for PostgreSQL by using Go's database/sql package.
- Google Cloud
- Cloud SQL for PostgreSQL
- Cloud SQL
- Go
Create a TCP connection with SSL certificates by using Node.js
Open a TCP connection to Cloud SQL for PostgreSQL by using the Node.js knex module with SSL (Secure Sockets Layer) certificates.
- Cloud SQL
- Google Cloud
- Cloud SQL for PostgreSQL
- Node JS
- JavaScript
Retry a failed connection when using Node.js
Retry failed connections when connecting to Cloud SQL for PostgreSQL by using the Node.js knex module.
- Cloud SQL for PostgreSQL
- Google Cloud
- Cloud SQL
- Node JS
- JavaScript
Add database flags
This sample adds database flags log_connections and log_min_error_statement.
- Google Cloud
- Cloud SQL for PostgreSQL
- Cloud SQL
- Terraform
Create a TCP connection by using PHP
Open a TCP connection to Cloud SQL for PostgreSQL by using PHP.
- Cloud SQL for PostgreSQL
- Google Cloud
- Cloud SQL
- PHP
Set the connection duration when using SQL Alchemy
Configure the maximum number of seconds a connection can persist when connecting to Cloud SQL for PostgreSQL by using SQLAlchemy.
- Cloud SQL for PostgreSQL
- Cloud SQL
- Google Cloud
- Python
Configure SSL certificates for TCP connection by using Python
Configure SSL (Secure Sockets Layer) certificates for a TCP connection to Cloud SQL for PostgreSQL by using Python's sqlalchemy package.
- Google Cloud
- Cloud SQL for PostgreSQL
- Cloud SQL
- Python
Set the connection duration when using ADO.NET
Configure the maximum number of seconds a connection can persist when connecting to Cloud SQL for PostgreSQL by using the ADO.NET Connection object.
- Cloud SQL
- Cloud SQL for PostgreSQL
- Google Cloud
- C#