gcppostgres

package
v0.48.2 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: Apache-2.0 Imports: 18 Imported by: 7

Documentation

Overview

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

URLs

For postgres.Open, gcppostgres registers for the scheme "gcppostgres". 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.

IP Type

By default, connections use auto-IP selection (public IP if available, otherwise private IP), matching the behavior of the legacy cloudsql-proxy. To use a specific IP type, set URLOpener.IPType or pass the "ip_type" query parameter in the URL (requires URLOpener.Dialer to be set):

gcppostgres://user:pass@project/region/instance/dbname?ip_type=psc

Valid values for ip_type: auto, public, private, psc.

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

Example
package main

import (
	"context"
	"log"

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

func main() {
	// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
	// PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/postgres/gcppostgres"
	// PRAGMA: On gocloud.dev, hide lines until the next blank line.
	ctx := context.Background()

	// Replace this with your actual settings.
	db, err := postgres.Open(ctx,
		"gcppostgres://user:password@example-project/region/my-instance01/testdb")
	if err != nil {
		log.Fatal(err)
	}
	defer func() { _ = db.Close() }()

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

Index

Examples

Constants

View Source
const Scheme = "gcppostgres"

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

Variables

This section is empty.

Functions

This section is empty.

Types

type URLOpener

type URLOpener struct {
	// Dialer creates Cloud SQL connections using the Cloud SQL Go Connector.
	// Supports PSC and explicit IP type selection via the "ip_type" URL query
	// parameter. If both Dialer and CertSource are set, Dialer takes precedence.
	Dialer *cloudsqlconn.Dialer

	// CertSource specifies how the opener will obtain authentication information.
	//
	// Deprecated: Use Dialer instead. CertSource does not support PSC or
	// explicit IP type selection. Ignored if Dialer is also set.
	CertSource proxy.CertSource

	// TraceOpts contains options for OpenTelemetry.
	TraceOpts []otelsql.Option
}

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

func (*URLOpener) OpenPostgresURL

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

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

Jump to

Keyboard shortcuts

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