Help us shape the future of Go! Take the 2026 Go Developer Survey

pgconn

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: ISC Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TruncateOutput

func TruncateOutput(out []byte) string

TruncateOutput caps subprocess output embedded in error messages. gRPC rejects messages that exceed its maximum frame size, so we keep only the first and last 1000 bytes, which contain the most useful diagnostic context.

func ValidDatabaseName

func ValidDatabaseName(name string) error

ValidDatabaseName reports whether name is safe to hand to a libpq client tool as a database name.

libpq treats a dbname containing "=" as a full conninfo string, so a value like `host=attacker.example dbname=x` redirects the connection -- and PGPASSWORD is already in the child's environment. A leading dash would be parsed as an option instead of an operand. Neither can appear in a real database name, so both are refused outright.

Types

type ConnConfig

type ConnConfig struct {
	Host     string
	Port     string
	Username string
	Password string

	SSLMode     string // disable, allow, prefer, require, verify-ca, verify-full
	SSLCert     string // path to client certificate file (PEM)
	SSLKey      string // path to client private key file (PEM)
	SSLRootCert string // path to root CA certificate file (PEM)
	// contains filtered or unexported fields
}

ConnConfig holds the connection parameters shared by all PostgreSQL connectors.

func ParseConnConfig

func ParseConnConfig(config map[string]string) (ConnConfig, string, error)

ParseConnConfig parses host, port, username, and password from the "location" URI and from standalone config keys. Standalone keys override URI components. The path component of the URI (i.e. the database name) is returned separately so callers can handle it as appropriate (use it, reject it, or ignore it).

func (ConnConfig) Args

func (c ConnConfig) Args() []string

Args returns the common connection flags shared by all PostgreSQL client tools: -h host -p port -w, plus -U username when one is configured.

func (*ConnConfig) Cleanup

func (c *ConnConfig) Cleanup()

Cleanup removes any temporary files that were created for inline SSL content. It should be called when the connector is closed.

func (ConnConfig) DSN

func (c ConnConfig) DSN(dbname string) string

DSN builds a PostgreSQL connection string for the given database.

func (ConnConfig) Env

func (c ConnConfig) Env() []string

Env returns the current process environment with PostgreSQL authentication and TLS variables injected when configured.

func (ConnConfig) Open

func (c ConnConfig) Open(dbname string) (*sql.DB, error)

Open returns a *sql.DB connected to the given database.

func (ConnConfig) Ping

func (c ConnConfig) Ping(ctx context.Context, connectDB string) error

Ping verifies connectivity by running a ping against the server. If connectDB is empty, "postgres" is used.

Jump to

Keyboard shortcuts

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