Help us shape the future of Go! Take the 2026 Go Developer Survey

pgengine

package
v1.9.1 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: MIT Imports: 12 Imported by: 0

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 ErrRuntimeUnavailable = errors.New("pgengine: container runtime unavailable")

ErrRuntimeUnavailable is returned when no container runtime is configured.

Functions

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