MySQL/PostgreSQL

Connecting to Cloud providers’ hosted database services requires additional steps to ensure the security of the connection. For example, each of GCP, AWS and Azure require the use of custom certificate authorities to be configured in the client. GCP requires a custom proxy with authentication credentials. The Go CDK makes opening these connections easier while still using the standard *sql.DB type.

Local or On-Premise🔗

The Go CDK uses the same URL opener pattern as seen in other Go CDK APIs. It differs from the standard library’s sql.Open in that it automatically instruments the connection with OpenCensus metrics.

The portable function for MySQL is mysql.Open:

import (
	"context"

	"gocloud.dev/mysql"
)

// Replace this with your actual settings.
db, err := mysql.Open(ctx, "mysql://user:password@localhost/testdb")
if err != nil {
	return err
}
defer db.Close()

// Use database in your program.
db.Exec("CREATE TABLE foo (bar INT);")

And the portable function for PostgreSQL is postgres.Open:

import (
	"context"

	"gocloud.dev/postgres"
)

// Replace this with your actual settings.
db, err := postgres.Open(ctx, "postgres://user:password@localhost/testdb")
if err != nil {
	return err
}
defer db.Close()

// Use database in your program.
db.Exec("CREATE TABLE foo (bar INT);")

GCP🔗

Users of GCP Cloud SQL for MySQL should import the gocloud.dev/mysql/gcpmysql package:

import (
	"context"

	"gocloud.dev/mysql"
	_ "gocloud.dev/mysql/gcpmysql"
)

// Replace this with your actual settings.
db, err := mysql.Open(ctx,
	"gcpmysql://user:password@example-project/region/my-instance01/testdb")
if err != nil {
	return err
}
defer db.Close()

// Use database in your program.
db.Exec("CREATE TABLE foo (bar INT);")

Users of GCP Cloud SQL for PostgreSQL should import the gocloud.dev/mysql/gcppostgres package:

import (
	"context"

	"gocloud.dev/postgres"
	_ "gocloud.dev/postgres/gcppostgres"
)

db, err := postgres.Open(ctx,
	"gcppostgres://myrole@example-project/us-central1/my-instance01/testdb")
if err != nil {
	return err
}
defer db.Close()

// Use database in your program.
db.Exec("CREATE TABLE foo (bar INT);")

AWS🔗

Users of AWS RDS for MySQL should import the gocloud.dev/mysql/awsmysql package:

import (
	"context"

	"gocloud.dev/mysql"
	_ "gocloud.dev/mysql/awsmysql"
)

// Replace these with your actual settings.
db, err := mysql.Open(ctx,
	"awsmysql://myrole:swordfish@example01.xyzzy.us-west-1.rds.amazonaws.com/testdb")
if err != nil {
	return err
}
defer db.Close()

// Use database in your program.
db.ExecContext(ctx, "CREATE TABLE foo (bar INT);")

Users of AWS RDS for PostgreSQL should import the gocloud.dev/postgres/awspostgres package:

import (
	"context"

	"gocloud.dev/postgres"
	_ "gocloud.dev/postgres/awspostgres"
)

// Replace these with your actual settings.
db, err := postgres.Open(ctx,
	"awspostgres://myrole:swordfish@example01.xyzzy.us-west-1.rds.amazonaws.com/testdb")
if err != nil {
	return err
}
defer db.Close()

// Use database in your program.
db.ExecContext(ctx, "CREATE TABLE foo (bar INT);")

Azure🔗

Users of Azure Database for MySQL should import the gocloud.dev/mysql/azuremysql package:

import (
	"context"

	"gocloud.dev/mysql"
	_ "gocloud.dev/mysql/azuremysql"
)

// Replace this with your actual settings.
db, err := mysql.Open(ctx,
	"azuremysql://user:password@example00.mysql.database.azure.com/testdb")
if err != nil {
	return err
}
defer db.Close()

// Use database in your program.
db.Exec("CREATE TABLE foo (bar INT);")

Other Usage Samples🔗