awspostgres

package
v0.46.0 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2026 License: Apache-2.0 Imports: 19 Imported by: 3

Documentation

Overview

Package awspostgres provides connections to AWS RDS PostgreSQL instances.

URLs

For postgres.Open, awspostgres registers for the scheme "awspostgres". The default URL opener will create a connection using the default credentials from the environment, as described in https://docs.aws.amazon.com/sdk-for-go/api/aws/session/. To customize the URL opener, or for more details on the URL format, see URLOpener.

To use IAM authentication, omit the password from the URL:

awspostgres://iam-user@myinstance.borkxyzzy.us-west-1.rds.amazonaws.com:5432/mydb

To specify an AWS profile or assume a role, add the following query parameters:

  • aws_profile: the AWS shared config profile to use
  • aws_role_arn: the ARN of the role to assume

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

Example
package main

import (
	"context"
	"log"

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

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/awspostgres"
	// PRAGMA: On gocloud.dev, hide lines until the next blank line.
	ctx := context.Background()

	// 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 {
		log.Fatal(err)
	}
	defer db.Close()

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

import (
	"context"
	"log"

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

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/awspostgres"
	// PRAGMA: On gocloud.dev, hide lines until the next blank line.
	ctx := context.Background()

	// To use IAM authentication, omit the password from the URL.
	// The IAM user or role must be granted rds_iam permission in the database.
	db, err := postgres.Open(ctx,
		"awspostgres://iamuser@example01.xyzzy.us-west-1.rds.amazonaws.com:5432/testdb")
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	// Use database in your program.
	if _, err := db.ExecContext(ctx, "CREATE TABLE IF NOT EXISTS foo (bar INT)"); err != nil {
		log.Fatal(err)
	}
	if _, err := db.ExecContext(ctx, "INSERT INTO foo (bar) VALUES ($1)", 42); err != nil {
		log.Fatal(err)
	}
	var val int
	if err := db.QueryRowContext(ctx, "SELECT bar FROM foo LIMIT 1").Scan(&val); err != nil {
		log.Fatal(err)
	}
}

Index

Examples

Constants

View Source
const Scheme = "awspostgres"

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

Variables

This section is empty.

Functions

This section is empty.

Types

type URLOpener

type URLOpener struct {
	// HTTPClient is the HTTP client used to fetch RDS certificates,
	// and IAM authentication tokens.
	HTTPClient *http.Client
	// CertSource specifies how the opener will obtain the RDS Certificate
	// Authority. If nil, it will use the default *rds.CertFetcher.
	CertSource rds.CertPoolProvider
	// TraceOpts contains options for OpenTelemetry.
	TraceOpts []otelsql.Option
}

URLOpener opens RDS PostgreSQL URLs like "awspostgres://user:password@myinstance.borkxyzzy.us-west-1.rds.amazonaws.com:5432/mydb".

To use IAM authentication, omit the password:

awspostgres://iam-user@myinstance.borkxyzzy.us-west-1.rds.amazonaws.com:5432/mydb

To specify an AWS profile or assume a role, add the following query parameters:

  • aws_profile: the AWS shared config profile to use
  • aws_role_arn: the ARN of the role to assume

func (*URLOpener) OpenPostgresURL

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

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

Jump to

Keyboard shortcuts

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