pgx

package
v1.8.1 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2026 License: MIT Imports: 2 Imported by: 0

README

sql/pgx

PostgreSQL-specific config helper for core/sql, backed by the pgx stdlib driver. Builds a libpq-format DSN from typed fields and registers the pgx driver via blank import.

Drop-in alternative to core/sql/postgres (lib/pq). Pick a driver by importing exactly one of these subpackages — this package never imports core/sql/postgres, so it does not pull in lib/pq. Use sql/pgx when you need pgx specifically: richer Postgres type handling, or libraries that assume pgx (e.g. River's database/sql driver).

See core/sql for the database wrapper itself; this subpackage only provides the connection-string helper.

Quickstart

import (
    "github.com/sergeyslonimsky/core/sql"
    "github.com/sergeyslonimsky/core/sql/pgx"
)

pgCfg := pgx.Config{
    Host:     "localhost",
    Port:     "5432",
    User:     "app",
    Password: "secret",
    Name:     "myapp",
    SSLMode:  "disable",
}

db, err := sql.New(ctx, sql.Config{
    DriverName: pgCfg.Driver(),
    DataSource: pgCfg.DSN(),
}, sql.WithOtel())

API

type Config struct {
    Host, Port, User, Password, Name, SSLMode string
}

func (Config) DSN() string     // libpq connection string (pgx parses keyword form)
func (Config) Driver() string  // "pgx"

Config is field-compatible with core/sql/postgres.Config, so switching drivers is a one-line import/type change. SSLMode defaults to "disable" when empty; other valid values: "require", "verify-ca", "verify-full" (libpq conventions).

Documentation

Overview

Package pgx provides a typed Config for PostgreSQL connections that produces a DSN string consumable by core/sql.New, registering the pgx stdlib driver.

Importing this package registers github.com/jackc/pgx/v5/stdlib via blank import under the database/sql driver name "pgx", so consuming code does not need a separate driver import. It is a drop-in alternative to core/sql/postgres (lib/pq) for consumers that need pgx — e.g. richer Postgres type support, or libraries that assume pgx (such as River's database/sql driver). Pick a driver by importing exactly one of these subpackages; this package never imports core/sql/postgres, so it does not pull in lib/pq.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Host     string
	Port     string
	User     string
	Password string
	Name     string

	// SSLMode follows libpq conventions: "disable", "require",
	// "verify-ca", "verify-full". Defaults to "disable" when empty.
	SSLMode string
}

Config describes a PostgreSQL connection. Plain fields, no struct tags — consumer apps map their viper keys to fields explicitly inside their own config.NewConfig(). Field-compatible with core/sql/postgres.Config so switching drivers is a one-line import/type change.

func (Config) DSN

func (c Config) DSN() string

DSN returns a libpq-format connection string. The pgx stdlib driver parses this keyword/value form, so the DSN is identical to core/sql/postgres. Use as the DataSource for sql.Config:

pgCfg := pgx.Config{...}
db, err := sql.New(ctx, sql.Config{
    DriverName: pgCfg.Driver(),
    DataSource: pgCfg.DSN(),
}, sql.WithOtel())

func (Config) Driver

func (c Config) Driver() string

Driver returns the database/sql driver name ("pgx"). Convenience for passing into sql.Config.DriverName.

Jump to

Keyboard shortcuts

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