gocloud.devgocloud.dev/postgres Index | Examples | Files | Directories

package postgres

import "gocloud.dev/postgres"

Package postgres provides functions to open PostgreSQL databases with OpenCensus instrumentation.

Index

Examples

Constants

const Scheme = "postgres"

Scheme is the URL scheme this package registers its URLOpener under on DefaultMux.

Functions

func Open

func Open(ctx context.Context, urlstr string) (*sql.DB, error)

Open opens the bucket identified by the URL given. URL openers must be registered in the DefaultURLMux, which is typically done in driver packages' initialization.

See the URLOpener documentation in driver subpackages for more details on supported scheme(s) and URL parameter(s).

Example

Code:play 

package main

import (
	"context"
	"log"

	"gocloud.dev/postgres"
)

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, hide lines until the next blank line.
	ctx := context.Background()

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

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

Types

type PostgresURLOpener

type PostgresURLOpener interface {
	// OpenPostgresURL opens a Postgres connection based on the URL.
	OpenPostgresURL(ctx context.Context, u *url.URL) (*sql.DB, error)
}

PostgresURLOpener can open Postgres connections based on a URL. The opener must not modify the URL argument. OpenPostgresURL must be safe to call from multiple goroutines.

This interface is generally implemented by types in driver packages.

type URLMux

type URLMux struct {
	// contains filtered or unexported fields
}

URLMux is a URL opener multiplexer. It matches the scheme of the URLs against a set of registered schemes and calls the opener that matches the URL's scheme.

The zero value is a multiplexer with no registered schemes.

func DefaultURLMux

func DefaultURLMux() *URLMux

DefaultURLMux returns the URLMux used by OpenPostgres.

Driver packages can use this to register their PostgresURLOpener on the mux.

func (*URLMux) OpenPostgres

func (mux *URLMux) OpenPostgres(ctx context.Context, urlstr string) (*sql.DB, error)

OpenPostgres calls OpenPostgresURL with the URL parsed from urlstr. OpenPostgres is safe to call from multiple goroutines.

func (*URLMux) OpenPostgresURL

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

OpenPostgresURL dispatches the URL to the opener that is registered with the URL's scheme. OpenPostgresURL is safe to call from multiple goroutines.

func (*URLMux) RegisterPostgres

func (mux *URLMux) RegisterPostgres(scheme string, opener PostgresURLOpener)

RegisterPostgres registers the opener with the given scheme. If an opener already exists for the scheme, RegisterPostgres panics.

type URLOpener

type URLOpener struct{}

URLOpener opens URLs like "postgres://" by using the underlying PostgreSQL driver. See https://godoc.org/github.com/lib/pq#hdr-Connection_String_Parameters for details.

func (*URLOpener) OpenPostgresURL

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

OpenPostgresURL opens a new database connection wrapped with OpenCensus instrumentation.

Source Files

postgres.go

Directories

PathSynopsis
postgres/awspostgresPackage awspostgres provides connections to AWS RDS PostgreSQL instances.
postgres/gcppostgresPackage gcppostgres provides connections to managed PostgreSQL Cloud SQL instances.
Version
v0.42.0 (latest)
Published
Jun 28, 2025
Platform
linux/amd64
Imports
7 packages
Last checked
2 hours ago

Tools for package owners.