pgconn

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package pgconn opens a Postgres bun.DB from a DSN or from the PG_* environment variables, for callers that do not already have a handle.

Both routes end in one pgdriver.WithDSN call, so the sslmode values accepted are exactly the ones pgdriver accepts, and an unsupported one is an error rather than a silent fallback.

Index

Constants

View Source
const (
	DefaultHost     = "localhost"
	DefaultPort     = "5432"
	DefaultDatabase = "postgres"
)

Defaults for the fields Config leaves empty, matching libpq's.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	DSN      string
	Host     string
	Port     string
	User     string
	Password string
	Database string
	SSLMode  string
}

Config is where to connect. DSN wins when set; otherwise the other fields are assembled into a postgres:// URL, with User and Password required and the rest defaulting as libpq does. SSLMode is passed through untouched, so the accepted values are pgdriver's: disable, allow, prefer, require, verify-ca and verify-full. Leaving it empty gets pgdriver's default, which is TLS without certificate verification.

func FromEnv

func FromEnv() Config

FromEnv reads DATABASE_URL into DSN and PG_HOST, PG_PORT, PG_USER, PG_PASSWORD, PG_DBNAME and PG_SSLMODE into the other fields. Nothing is validated here; Config.URL and Config.Open report what is missing.

func (Config) Open

func (c Config) Open() (db *bun.DB, err error)

Open returns a bun.DB for c. It does not dial: the first query does, so a wrong password surfaces there rather than here. A DSN pgdriver cannot parse is an error here.

func (Config) URL

func (c Config) URL() (string, error)

URL returns the DSN Config.Open will use: DSN itself when set, otherwise a postgres:// URL built from the other fields. It is an error for User or Password to be empty when DSN is.

Jump to

Keyboard shortcuts

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