postgres

package
v1.3.3 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package postgres provides common code for working with the Postgres database in the context of PGQS service.

Index

Constants

This section is empty.

Variables

View Source
var ErrConfig = errors.New("invalid configuration")

ErrConfig is the error returned when the configuration is invalid.

Functions

func MapNotificationToLogFields

func MapNotificationToLogFields(n *Notifications) []logger.Field

MapNotificationToLogFields maps the notification to a slice of logger fields.

Types

type Config

type Config struct {
	User string `name:"user" flag:"-"`
	//nolint:gosec // G117: credential loaded from env via configs.Resolve; never logged verbatim (MapToLogFields).
	Pass     string `name:"pass" flag:"-"`
	Database string `name:"database" flag:"-"`
	Host     string `name:"host" default:"127.0.0.1" flag:"-"`
	SSLMode  string `name:"ssl_mode" default:"disable" flag:"-"`
	Port     int    `name:"port" default:"5432" flag:"-"`
}

Config represents the configuration for the Postgres database.

func ResolveConfig

func ResolveConfig() (*Config, error)

ResolveConfig resolves the configuration for the Postgres database. Expecting only env variables with prefix "PG_". Returns the configuration and an error if the configuration is invalid.

func (*Config) DBUrl

func (cfg *Config) DBUrl(driverOpt ...string) string

DBUrl returns the database URL for the Postgres database.

func (*Config) MapToLogFields

func (cfg *Config) MapToLogFields() []logger.Field

MapToLogFields maps the configuration to a slice of logger fields.

type DBConn

type DBConn = pgxpool.Conn

type DBPool

type DBPool = pgxpool.Pool

func NewPool

func NewPool(ctx context.Context, dbURL string) (*DBPool, error)

NewPool initializes and returns a new database connection pool using the provided context and configuration. It validates the context and configuration, establishes a connection pool, and performs initial health checks. If any step fails, an error is returned.

type Dialect

type Dialect = goqu.DialectWrapper

func SQL

func SQL() Dialect

SQL returns the SQL dialect for the Postgres database.

type Listener

type Listener struct {
	// contains filtered or unexported fields
}

func NewListener

func NewListener(conn *DBConn, channel string, notificationBuffer int) *Listener

NewListener creates a new Listener for channel on conn. notificationBuffer is the capacity of Notifications(); values less than 1 are treated as 1.

func NewListenerFromPool

func NewListenerFromPool(ctx context.Context, pool *DBPool, channel string, notificationBuffer int) (*Listener, error)

NewListenerFromPool acquires a connection from pool and returns a Listener for channel. notificationBuffer is the capacity of Notifications(); values less than 1 are treated as 1.

func (*Listener) Err

func (l *Listener) Err() error

func (*Listener) Notifications

func (l *Listener) Notifications() <-chan *Notifications

Notifications returns a channel to receive database notifications from the listener asynchronously.

func (*Listener) Start

func (l *Listener) Start(ctx context.Context) error

Start initializes the listener to begin receiving notifications on the specified channel. Returns an error if failed.

func (*Listener) Stop

func (l *Listener) Stop(timeout time.Duration)

Stop gracefully stops the listener by canceling the context and releasing the database connection.

type Notifications

type Notifications = pgconn.Notification

Jump to

Keyboard shortcuts

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