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.
Use ActiveRecord to manage connections
Insert data by using ActiveRecord to manage opening and closing a connection to Cloud SQL for PostgreSQL.
- Cloud SQL for PostgreSQL
- Cloud SQL
- Google Cloud
- Ruby
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
- Cloud SQL for PostgreSQL
- 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
- Cloud SQL for PostgreSQL
- Google Cloud
- 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.
- Google Cloud
- Cloud SQL
- Cloud SQL for PostgreSQL
- Node JS
- JavaScript
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 for PostgreSQL
- 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
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.
- Google Cloud
- Cloud SQL for PostgreSQL
- 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
- Cloud SQL for PostgreSQL
- Google Cloud
- Java
Create an instance with password policy enabled
This sample creates an instance with password policy enabled.
- Cloud SQL
- Google Cloud
- 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
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
- Google Cloud
- Cloud SQL for PostgreSQL
- Python
Create Postgres Private IP
Sample demonstrating how to create the private IP for use with a Postgres Database.
- Cloud SQL for PostgreSQL
- Cloud SQL
- Google Cloud
- Terraform
Retry a failed connection when using SQLAlchemy
Retry failed connections when connecting to Cloud SQL for PostgreSQL by using SQLAlchemy.
- Cloud SQL
- Google Cloud
- Cloud SQL for PostgreSQL
- 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.
- Cloud SQL
- Cloud SQL for PostgreSQL
- Google Cloud
- PHP
Upgrade a instance to the target maintenance version
This sample upgrades a instance to the target maintenance version POSTGRES_14_4.R20220710.01_07.
- Google Cloud
- Cloud SQL for PostgreSQL
- Cloud SQL
- Terraform
Create a TCP connection by using Java
Open a TCP connection to Cloud SQL for PostgreSQL by using Java.
- Google Cloud
- Cloud SQL for PostgreSQL
- Cloud SQL
- Java
Create a socket connection by using PHP
Open a Unix socket connection to Cloud SQL for PostgreSQL by using PHP.
- Cloud SQL for PostgreSQL
- Google Cloud
- Cloud SQL
- PHP
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 for PostgreSQL
- Google Cloud
- Cloud SQL
- C#
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.
- Cloud SQL
- Cloud SQL for PostgreSQL
- Google Cloud
- 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 for PostgreSQL
- Google Cloud
- Cloud SQL
- Go
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.
- Google Cloud
- Cloud SQL
- Cloud SQL for PostgreSQL
- JavaScript
- Node JS
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 for PostgreSQL
- Cloud SQL
- 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.
- Google Cloud
- Cloud SQL
- Cloud SQL for PostgreSQL
- C#
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
- Google Cloud
- Cloud SQL for PostgreSQL
- Node JS
- JavaScript
Configure public IP for a instance
This sample enable public IP and adds an authorized address range for a instance.
- Google Cloud
- Cloud SQL for PostgreSQL
- Cloud SQL
- Terraform
Add database flags
This sample adds database flags log_connections and log_min_error_statement.
- 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 for PostgreSQL
- Cloud SQL
- 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
- Cloud SQL for PostgreSQL
- Google Cloud
- C#
Create a TCP connection by using PHP
Open a TCP connection to Cloud SQL for PostgreSQL by using PHP.
- Cloud SQL for PostgreSQL
- Cloud SQL
- Google Cloud
- PHP
Set the connection timeout when using SQLAlchemy
Configure a connection timeout when connecting to Cloud SQL for PostgreSQL by using SQLAlchemy.
- Google Cloud
- Cloud SQL
- 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.
- Google Cloud
- Cloud SQL for PostgreSQL
- Cloud SQL
- 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.
- Google Cloud
- Cloud SQL
- Cloud SQL for PostgreSQL
- Python
Schedule automated backups for an instance
This sample schedules automated backups for an instance starting within 4 hours from 20:55.
- Google Cloud
- Cloud SQL for PostgreSQL
- Cloud SQL
- 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
- Cloud SQL for PostgreSQL
- Google Cloud
- Node JS
- JavaScript
Configure authorized networks
This sample configures authorized networks to IP address range 192.0.2.0/24.
- Google Cloud
- Cloud SQL
- Cloud SQL for PostgreSQL
- 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
- Cloud SQL for PostgreSQL
- Google Cloud
- JavaScript
- Node JS
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.
- Google Cloud
- Cloud SQL for PostgreSQL
- Cloud SQL
- 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.
- Cloud SQL
- Google Cloud
- Cloud SQL for PostgreSQL
- Python
Create a TCP connection by using Node.js
Opens a TCP connection to Cloud SQL for PostgreSQL by using the Node.js knex module.
- Google Cloud
- Cloud SQL
- 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.
- Google Cloud
- Cloud SQL
- 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 for PostgreSQL
- Cloud SQL
- Google Cloud
- Java
Create a socket connection by using Python
Open a Unix socket connection to Cloud SQL for PostgreSQL by using Python.
- 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.
- Cloud SQL
- Cloud SQL for PostgreSQL
- Google Cloud
- 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 TCP connection by using ADO.NET
Open a TCP connection to Cloud SQL for PostgreSQL by using an ADO.NET Connection object.
- Cloud SQL
- Cloud SQL for PostgreSQL
- 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.
- Cloud SQL for PostgreSQL
- Google Cloud
- Cloud SQL
- 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.
- Google Cloud
- Cloud SQL for PostgreSQL
- Cloud SQL
- 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.
- Google Cloud
- Cloud SQL
- Cloud SQL for PostgreSQL
- PHP
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 for PostgreSQL
- Cloud SQL
- Google Cloud
- C#
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
- Cloud SQL for PostgreSQL
- Google Cloud
- Go