tmppg

package module
v0.0.0-...-47d05a5 Latest Latest
Warning

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

Go to latest
Published: May 5, 2026 License: MIT Imports: 16 Imported by: 0

README

Temporary PostgreSQL instances for Go

tmppg locally spawns a new PostgreSQL database cluster for you to create databases in, mostly for tests. Since initializing a database cluster is relatively expensive, it can be done in a wrapper program: go run github.com/authenticvision/tmppg/cmd/wrapper@latest -- go test ./...

tmppg is a reimplementation of github.com/stapelberg/postgrestest with different opinions:

  • Run postgres directly instead of via pg_ctl
  • Pass config to postgres via CLI arguments instead of using a config file
  • No Windows support
  • An effectively worse startup check
  • Use pgx instead of database/sql

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RunWithPostgresql

func RunWithPostgresql(args []string, opts ...PostgresOption) error

RunWithPostgresql runs the given command with a temporary PostgreSQL instance available. Connection information is available via the standard PG* environment variables. See https://www.postgresql.org/docs/current/libpq-envars.html

func RunWithPostgresqlDir

func RunWithPostgresqlDir(dir string, args []string, opts ...PostgresOption) error

RunWithPostgresqlDir runs the given command with a PostgreSQL instance available. Data is persisted in the given directory. A cluster is initialized if it doesn't exist. Connection information is available via the standard PG* environment variables. See https://www.postgresql.org/docs/current/libpq-envars.html

func WithPostgresql

func WithPostgresql(fn func(socketDir string) error, opts ...PostgresOption) error

WithPostgresql runs fn after spawning a temporary Postgresql instance

func WithPostgresqlDir

func WithPostgresqlDir(dir string, fn func(socketDir string) error, opts ...PostgresOption) error

WithPostgresqlDir runs fn after spawning a Postgresql instance using dir as its data and socket directory. The cluster will be created in dir if it doesn't exist or is empty

Types

type Cluster

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

func CreateCluster

func CreateCluster(pg *Postgres, dir string) (*Cluster, error)

func OpenCluster

func OpenCluster(pg *Postgres, dir string) (*Cluster, error)

func OpenOrCreateCluster

func OpenOrCreateCluster(pg *Postgres, dir string) (*Cluster, error)

func (*Cluster) Start

func (c *Cluster) Start() error

func (*Cluster) Stop

func (c *Cluster) Stop() error

func (*Cluster) URL

func (c *Cluster) URL() *url.URL

type DatabaseOption

type DatabaseOption func(pg *pgxpool.Config) error

type Instance

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

func NewInstance

func NewInstance(connString string) *Instance

func (*Instance) WithDatabase

func (i *Instance) WithDatabase(ctx context.Context, fn func(pool *pgxpool.Pool) error, opts ...DatabaseOption) (err error)

func (*Instance) WithDatabaseSchema

func (i *Instance) WithDatabaseSchema(ctx context.Context, schemaSQL string, fn func(pool *pgxpool.Pool) error, opts ...DatabaseOption) error

type Postgres

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

func NewPostgres

func NewPostgres(opts ...PostgresOption) *Postgres

type PostgresOption

type PostgresOption func(pg *Postgres)

func WithLogOutput

func WithLogOutput(logger *slog.Logger, level slog.Level) PostgresOption

func WithLogStatement

func WithLogStatement(setting string) PostgresOption

WithLogStatement configures postgres' log_statement setting. Possible values are none, ddl, mod and all. Defaults to none.

func WithOutput

func WithOutput(stdout, stderr io.Writer) PostgresOption

func WithoutSync

func WithoutSync() PostgresOption

WithoutSync allows disabling syncing if data integrity isn't important

type UninitializedError

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

func (*UninitializedError) Error

func (e *UninitializedError) Error() string

func (*UninitializedError) Unwrap

func (e *UninitializedError) Unwrap() error

Directories

Path Synopsis
cmd
tmppg command
internal

Jump to

Keyboard shortcuts

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