gocloud.dev: gocloud.dev/postgres/cloudpostgres Index | Examples | Files

package cloudpostgres

import "gocloud.dev/postgres/cloudpostgres"

Package cloudpostgres provides connections to managed PostgreSQL Cloud SQL instances. See https://cloud.google.com/sql/docs/postgres/ for more information.


For postgres.Open, cloudpostgres registers for the scheme "cloudpostgres". The default URL opener will create a connection using the default credentials from the environment, as described in https://cloud.google.com/docs/authentication/production. To customize the URL opener, or for more details on the URL format, see URLOpener.

See https://gocloud.dev/concepts/urls/ for background information.


ctx := context.Background()
creds, err := google.FindDefaultCredentials(ctx, "https://www.googleapis.com/auth/cloud-platform")
if err != nil {
authClient := gcp.HTTPClient{Client: *oauth2.NewClient(ctx, creds.TokenSource)}
db, _, err := cloudpostgres.Open(ctx, cloudsql.NewCertSource(&authClient), &cloudpostgres.Params{
    // Replace these with your actual settings.
    ProjectID: "example-project",
    Region:    "us-central1",
    Instance:  "my-instance01",
    User:      "myrole",
    Database:  "test",
if err != nil {

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



Package Files



const Scheme = "cloudpostgres"

Scheme is the URL scheme cloudpostgres registers its URLOpener under on postgres.DefaultMux.

func Open Uses

func Open(ctx context.Context, certSource proxy.CertSource, params *Params) (*sql.DB, func(), error)

Open opens a Cloud SQL database. The second return value is a cleanup function that calls Close on the returned database.

type Params Uses

type Params struct {
    // ProjectID is the project the instance is located in.
    ProjectID string
    // Region is the region the instance is located in.
    Region string
    // Instance is the name of the instance.
    Instance string

    // User is the database user to connect as.
    User string
    // Password is the database user password to use.
    Password string
    // Database is the PostgreSQL database name to connect to.
    Database string

    // Values sets additional parameters, as documented in
    // https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PARAMKEYWORDS.
    Values url.Values

    // TraceOpts contains options for OpenCensus.
    TraceOpts []ocsql.TraceOption

Params specifies how to connect to a Cloud SQL database.

type URLOpener Uses

type URLOpener struct {
    // CertSource specifies how the opener will obtain authentication information.
    // CertSource must not be nil.
    CertSource proxy.CertSource

    // TraceOpts contains options for OpenCensus.
    TraceOpts []ocsql.TraceOption

URLOpener opens GCP PostgreSQL URLs like "cloudpostgres://user:password@myproject/us-central1/instanceId/mydb".

func (*URLOpener) OpenPostgresURL Uses

func (uo *URLOpener) OpenPostgresURL(ctx context.Context, u *url.URL) (*sql.DB, error)

OpenPostgresURL opens a new GCP database connection wrapped with OpenCensus instrumentation.

Package cloudpostgres imports 16 packages (graph). Updated 2019-05-09. Refresh now. Tools for package owners.