pgdb

package
v0.5.6 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package pgdb opens a PostgreSQL connection pool and helps with transactions.

It stays deliberately thin: queries are written by generators (sqlc for static ones, jet for dynamic ones) and only the parts identical in every project live here.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InTx

func InTx(ctx context.Context, pool *pgxpool.Pool, fn func(tx pgx.Tx) error) error

InTx runs fn inside a transaction: it rolls back on error or panic and commits otherwise.

func Migrate added in v0.2.0

func Migrate(ctx context.Context, pool *pgxpool.Pool, fsys fs.FS, log *slog.Logger) ([]string, error)

Migrate applies the pending SQL migrations from fsys and returns the files it applied.

Migrations are goose files ("-- +goose Up"). A session advisory lock is held while they run, so several instances starting at once apply each migration exactly once. Files other than *.sql are ignored: the embedded directory also holds its Go file.

func Open

func Open(ctx context.Context, cfg Config) (*pgxpool.Pool, error)

Open creates the pool and verifies the connection. Without the check the service would start against an unreachable database and fail later, on a user request.

Types

type Config

type Config struct {
	URL             string
	MaxConns        int32
	MinConns        int32
	MaxConnLifetime time.Duration
	MaxConnIdleTime time.Duration
	ConnectTimeout  time.Duration
}

Config holds pool settings. Empty fields fall back to defaults.

Jump to

Keyboard shortcuts

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