pgengine

package
v1.10.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: MIT Imports: 16 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package pgengine runs real PostgreSQL servers in containers for emulators of managed PostgreSQL services: a named data volume, a published loopback port, SQL access as an engine-private superuser, and stop/start that keep data.

Index

Constants

View Source
const (
	// Superuser is the engine-private role; emulators keep its password to themselves.
	Superuser = "gsroot"
)

Variables

View Source
var (
	// ErrInvalidParameter reports a parameter name or value the engine refuses to put into SQL.
	ErrInvalidParameter = errors.New("pgengine: invalid parameter")
	// ErrInvalidDatabase reports a database name that cannot be passed to pg_dump safely.
	ErrInvalidDatabase = errors.New("pgengine: invalid database name")
	// ErrExecUnsupported is returned when the container runtime cannot relay standard input to a command.
	ErrExecUnsupported = errors.New("pgengine: the container runtime cannot run commands with standard input")
)
View Source
var ErrRuntimeUnavailable = errors.New("pgengine: container runtime unavailable")

ErrRuntimeUnavailable is returned when no container runtime is configured.

Functions

func CheckParameter

func CheckParameter(name, value string, reset bool) error

CheckParameter reports whether SetParameter would accept the name and value, without touching a server.

func Ident

func Ident(name string) string

Ident quotes a SQL identifier.

func Literal

func Literal(v string) string

Literal quotes a SQL string literal (standard_conforming_strings is on in the official images).

Types

type Config

type Config struct {
	Runtime      Runtime
	Volumes      container.VolumeRuntime
	Ports        *portalloc.Allocator
	Logger       *slog.Logger
	Prefix       string
	StartTimeout time.Duration
}

Config configures an Engine. Prefix names containers and volumes ("<prefix>-...").

type Engine

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

Engine launches PostgreSQL servers.

func New

func New(cfg Config) (*Engine, error)

New validates cfg and returns an Engine.

func (*Engine) Launch

func (e *Engine) Launch(ctx context.Context, spec Spec) (*Server, error)

Launch starts a server and waits until it accepts superuser connections.

func (*Engine) RemoveVolume

func (e *Engine) RemoveVolume(ctx context.Context, id string)

RemoveVolume deletes the data volume of server id.

func (*Engine) VolumeName

func (e *Engine) VolumeName(id string) string

VolumeName is the data volume of server id.

type Runtime

type Runtime interface {
	CreateAndStart(ctx context.Context, spec container.Spec) (string, error)
	StopAndRemove(ctx context.Context, containerID string) error
	StopContainer(ctx context.Context, containerID string) error
	StartContainer(ctx context.Context, containerID string) error
}

Runtime is the slice of the container runtime the engine uses.

type Server

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

Server is one running PostgreSQL container.

func (*Server) Close

func (s *Server) Close(ctx context.Context, purge bool)

Close removes the container and releases its port; purge also deletes the data volume.

func (*Server) CreateDatabase

func (s *Server) CreateDatabase(ctx context.Context, name, owner, charset, collation string) error

CreateDatabase creates a database owned by owner; an existing one is left alone.

func (*Server) Databases

func (s *Server) Databases(ctx context.Context) ([]string, error)

Databases lists the non-template databases.

func (*Server) DropDatabase

func (s *Server) DropDatabase(ctx context.Context, name string) error

DropDatabase drops a database, disconnecting its sessions; a missing one is not an error.

func (*Server) DropRole

func (s *Server) DropRole(ctx context.Context, user string) error

DropRole removes a login role; a missing one is not an error.

func (*Server) Dump

func (s *Server) Dump(ctx context.Context, w io.Writer, dbs []string, role string) error

Dump streams a logical dump of dbs to w: for each database a \connect line, SET ROLE role when role is not empty, and the pg_dump output without ownership or privileges. Names are checked before they reach a command.

func (*Server) Endpoint

func (s *Server) Endpoint() (string, int)

Endpoint is the host and published port clients connect to.

func (*Server) EnsureAdmin

func (s *Server) EnsureAdmin(ctx context.Context, user, password string) error

EnsureAdmin creates or updates a login role that can create databases and roles and owns what it creates.

func (*Server) Exec

func (s *Server) Exec(ctx context.Context, db, stmt string) error

Exec runs one statement as the superuser in database db ("" for the default). Callers quote identifiers and literals with Ident and Literal.

func (*Server) ReadOnly

func (s *Server) ReadOnly(ctx context.Context, on bool) error

ReadOnly sets or clears default_transaction_read_only for the whole server.

func (*Server) ReloadConf

func (s *Server) ReloadConf(ctx context.Context) error

ReloadConf makes the server re-read its configuration files.

func (*Server) Restore

func (s *Server) Restore(ctx context.Context, r io.Reader) error

Restore runs a Dump stream in the server as the superuser and stops at the first error.

func (*Server) SetParameter

func (s *Server) SetParameter(ctx context.Context, name, value string, reset bool) error

SetParameter persists a server parameter with ALTER SYSTEM; reset removes it. Dynamic parameters take effect after ReloadConf, static ones after the next restart. name must be a lower-case parameter name and value printable text without backslashes.

func (*Server) Start

func (s *Server) Start(ctx context.Context) error

Start starts a stopped server and waits until it accepts connections.

func (*Server) Stop

func (s *Server) Stop(ctx context.Context) error

Stop stops the container, keeping its data and published port.

type Spec

type Spec struct {
	// ID names the container and, when Persist is set, the data volume.
	ID string
	// Image is the container image, e.g. postgres:16.
	Image string
	// Password is the superuser password applied on first initialisation; it must match an existing volume.
	Password string
	// InitdbArgs is passed to initdb on first initialisation (POSTGRES_INITDB_ARGS); trusted, not user input.
	InitdbArgs string
	Persist    bool
	Fresh      bool
}

Spec describes one server.

Jump to

Keyboard shortcuts

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