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.

Code sample

Go

var opts []cloudsqlconn.Option
if dbIAMUser != "" {
	opts = append(opts, cloudsqlconn.WithIAMAuthN())
}
if usePrivate != "" {
	opts = append(opts, cloudsqlconn.WithDefaultDialOptions(cloudsqlconn.WithPrivateIP()))
}
d, err := cloudsqlconn.NewDialer(context.Background(), opts...)
if err != nil {
	return nil, err
}

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.