Jump to Content

How to Connect your Go Application to Cloud SQL

March 24, 2023
Luke Schlangen

Developer Advocate, Google Cloud

Jack Wotherspoon

Software Engineer

The Cloud SQL Go connector is the easiest way to securely connect your Go application to your Cloud SQL database. If you are writing a Go application, we recommend using the Cloud SQL Go connector.

When to use the Cloud SQL Go connector

Cloud SQL has two types of connectors:

  1. Language-specific connectors - Available for Java, Python, Go, and NodeJS.

  2. Cloud SQL Auth proxy - A good choice when a language-specific connector is not available.

These connectors come with significant advantages over a traditional database connection. We laid out a few of the benefits of using a connector in our previous blog post: Understanding Cloud SQL connectors. With code-level control, the Cloud SQL Go connector is the most convenient way to securely connect your Go Application to your database.


Try it out

The GitHub ReadMe includes usage documentation for PostgreSQL, MySQL, and SQL Server for integrating the connector into your application. To show just how easy it can be, we’ve released a Codelab for How to connect a Go application on Cloud Run to a Cloud SQL for PostgreSQL database.

In a single `main.go` file, this application:

  • accepts HTTP requests

  • securely connects to the Cloud SQL for PostgreSQL database using a service account with automatic IAM database authentication

  • stores the time of the HTTP request in the database

  • returns the times of the last five visits


With the Go Connector, you get all the best-practices of connecting securely out of the box.

AlloyDB Connector

One last exciting note. Go is the first language to have a language-specific connector for AlloyDB. The AlloyDB Go connector is built on the Cloud SQL connector, so with minimal changes to swap out the library, the code you write for Cloud SQL will work if you ever decide to use AlloyDB.

Posted in