cloudpostgres

package
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 13, 2018 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

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

Example
package main

import (
	"context"

	"github.com/google/go-cloud/gcp"
	"github.com/google/go-cloud/gcp/cloudsql"
	"github.com/google/go-cloud/postgres/cloudpostgres"
	"golang.org/x/oauth2"
	"golang.org/x/oauth2/google"
)

func main() {
	ctx := context.Background()
	creds, err := google.FindDefaultCredentials(ctx, "https://www.googleapis.com/auth/cloud-platform")
	if err != nil {
		panic(err)
	}
	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 {
		panic(err)
	}

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

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Open

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

Open opens a Cloud SQL database.

Types

type Params

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.
	// May be empty, see https://cloud.google.com/sql/docs/sql-proxy#user
	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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL