postgres

package
v1.6.2 Latest Latest
Warning

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

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

README

sql/postgres

PostgreSQL-specific config helper for core/sql. Builds a libpq-format DSN from typed fields and registers the lib/pq driver via blank import.

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/postgres"
)

pgCfg := postgres.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
func (Config) Driver() string  // "postgres"

SSLMode defaults to "disable" when empty. Other valid values: "require", "verify-ca", "verify-full" (libpq conventions).

Documentation

Overview

Package postgres provides a typed Config for PostgreSQL connections that produces a DSN string consumable by core/sql.New.

Importing this package also registers the lib/pq driver via blank import, so consuming code does not need a separate driver import.

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().

func (Config) DSN added in v1.3.0

func (c Config) DSN() string

DSN returns a libpq-format connection string. Use as the DataSource for sql.Config:

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

func (Config) Driver added in v1.3.0

func (c Config) Driver() string

Driver returns the database/sql driver name ("postgres"). 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