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
- Variables
- func CheckParameter(name, value string, reset bool) error
- func Ident(name string) string
- func Literal(v string) string
- type Config
- type Engine
- type Runtime
- type Server
- func (s *Server) Close(ctx context.Context, purge bool)
- func (s *Server) CreateDatabase(ctx context.Context, name, owner, charset, collation string) error
- func (s *Server) Databases(ctx context.Context) ([]string, error)
- func (s *Server) DropDatabase(ctx context.Context, name string) error
- func (s *Server) DropRole(ctx context.Context, user string) error
- func (s *Server) Dump(ctx context.Context, w io.Writer, dbs []string, role string) error
- func (s *Server) Endpoint() (string, int)
- func (s *Server) EnsureAdmin(ctx context.Context, user, password string) error
- func (s *Server) Exec(ctx context.Context, db, stmt string) error
- func (s *Server) ReadOnly(ctx context.Context, on bool) error
- func (s *Server) ReloadConf(ctx context.Context) error
- func (s *Server) Restore(ctx context.Context, r io.Reader) error
- func (s *Server) SetParameter(ctx context.Context, name, value string, reset bool) error
- func (s *Server) Start(ctx context.Context) error
- func (s *Server) Stop(ctx context.Context) error
- type Spec
Constants ¶
const (
// Superuser is the engine-private role; emulators keep its password to themselves.
Superuser = "gsroot"
)
Variables ¶
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") )
ErrRuntimeUnavailable is returned when no container runtime is configured.
Functions ¶
func CheckParameter ¶
CheckParameter reports whether SetParameter would accept the name and value, without touching a server.
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 (*Engine) RemoveVolume ¶
RemoveVolume deletes the data volume of server id.
func (*Engine) VolumeName ¶
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 ¶
Close removes the container and releases its port; purge also deletes the data volume.
func (*Server) CreateDatabase ¶
CreateDatabase creates a database owned by owner; an existing one is left alone.
func (*Server) DropDatabase ¶
DropDatabase drops a database, disconnecting its sessions; a missing one is not an error.
func (*Server) Dump ¶
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) EnsureAdmin ¶
EnsureAdmin creates or updates a login role that can create databases and roles and owns what it creates.
func (*Server) Exec ¶
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 ¶
ReadOnly sets or clears default_transaction_read_only for the whole server.
func (*Server) ReloadConf ¶
ReloadConf makes the server re-read its configuration files.
func (*Server) Restore ¶
Restore runs a Dump stream in the server as the superuser and stops at the first error.
func (*Server) SetParameter ¶
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.
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.