Documentation
¶
Overview ¶
Package pgdump dumps a Postgres database by running PostgreSQL's own pg_dump (and, for the split format, pg_restore) against a dbc connection — `dbc dump` on the command line.
It is UI-free, like pgdocker: the CLI (dumpcmd.go) turns flags into Options, finds the tools with Locate, and hands both to a Run.
WHY pg_dump AND NOT A DUMPER OF dbc's OWN. A correct dump is a lot more than tables and rows: dependency ordering, ownership and grants, extensions, sequences' positions, large objects, a consistent snapshot across parallel workers, and a format pg_restore reads back. pg_dump does all of it and is the format every Postgres user already trusts. What dbc adds is the connection — the one already configured, TLS and derived databases included (db.LibpqFor) — finding a pg_dump new enough for the server, and a split format pg_dump does not have.
THE FORMATS:
plain one .sql file (or stdout), for psql
custom one compressed archive, for pg_restore (selective restore)
tar one tar archive, for pg_restore
directory one file per table plus a table of contents, for pg_restore;
the only format pg_dump can write with parallel --jobs
split dbc's: plain SQL, one file per table, for psql and for
reading or diffing — see split.go
Index ¶
- Variables
- func BinDir(flag string, cfg *config.Config) string
- func CheckOutDir(dir string) error
- func DefaultOut(conn string, f Format, now time.Time) string
- func HumanSize(n int64) string
- func IsPostgres(driver string) bool
- func ResolveOut(p string) (string, error)
- func RestoreHint(f Format, out string) string
- func ServerMajor(ctx context.Context, mgr *db.Manager, conn string) (int, error)
- func Size(p string) int64
- func SwapSuffix(p string, from, to Format) string
- func Target(cfg *config.Config, conn string) (db.LibpqConn, error)
- type Form
- type Format
- type LineWriter
- type Options
- type Run
- type Tools
Constants ¶
This section is empty.
Variables ¶
var ErrNoConn = errors.New("no such connection")
ErrNoConn is a dump asked of a connection name the config does not have.
var ErrNotPostgres = errors.New("dumps run pg_dump, which is for Postgres connections")
ErrNotPostgres is a dump asked of a connection that is not Postgres.
Formats lists every format, for help text and messages.
Functions ¶
func BinDir ¶
BinDir is where to look for the tools: flag (the CLI's --pg-bin, which reads $DBC_PG_BIN itself), else $DBC_PG_BIN, else the config's pg_bin, else "" — search (Locate).
func CheckOutDir ¶
CheckOutDir reports whether a directory-writing format (directory, split) may write into dir: it must not exist yet or be empty, as pg_dump asks of a directory-format target — files already there would mix two dumps. The CLI calls it before connecting; split calls it again (via prepareOutDir) when it starts.
func DefaultOut ¶
DefaultOut is where the dialogs suggest writing conn's dump: Downloads (else the home directory), named for the connection and the minute — "~/Downloads/prod_analytics-20261009-1430.sql" — so a second dump never lands on the first.
func IsPostgres ¶
IsPostgres reports whether driver (any alias db.Driver accepts) is Postgres — whether a connection can be dumped.
func ResolveOut ¶
ResolveOut makes a typed output path absolute: ~ is the home directory, and a relative path is relative to dbc's working directory. Said in full in the log afterwards, so where the dump went is never a guess.
func RestoreHint ¶
RestoreHint is the command that reads a dump of format f at out back into a database.
func ServerMajor ¶
ServerMajor asks conn's server its major version: server_version_num is 170002 for 17.2 and 90624 for 9.6.24 — whose major, for pg_dump's version check, is 9. It doubles as the connection check, so a refused password is said once, by dbc, rather than by every pg_dump worker.
func SwapSuffix ¶
SwapSuffix moves a path written for one format to another's suffix, when it still ends with the first's — the dialogs call it as the format changes, so the suggested "x.sql" becomes "x.dump", while a name the user typed themselves is left alone.
Types ¶
type Form ¶
type Form struct {
Format string `json:"format"` // a ParseFormat name
Out string `json:"out"` // ~ and relative paths allowed (ResolveOut)
Jobs string `json:"jobs"` // "" for 1
// Content is "" (everything), "schema" or "data".
Content string `json:"content"`
// The filters: pg_dump patterns, separated by commas or blanks; a
// pattern with a blank in it is "quoted".
Schemas string `json:"schemas"`
Tables string `json:"tables"`
ExcludeTables string `json:"exclude_tables"`
ExcludeData string `json:"exclude_data"`
NoOwner bool `json:"no_owner"`
NoPrivileges bool `json:"no_privileges"`
Inserts bool `json:"inserts"`
Create bool `json:"create"`
Clean bool `json:"clean"`
// Extra is any other pg_dump options, as on a command line.
Extra string `json:"extra"`
}
Form is the UIs' dump dialog as typed: text as entered, checkboxes as ticked. Options turns it into Options; the JSON names are dbc web's.
type Format ¶
type Format string
Format is the shape of a dump.
func ParseFormat ¶
ParseFormat reads a format name, pg_dump's one-letter ones included. "text", the global -t flag's default, is plain: it is the dump's text form.
func (Format) Archive ¶
Archive reports whether f is one of pg_dump's archive formats, read back with pg_restore rather than psql.
type LineWriter ¶
type LineWriter struct {
// contains filtered or unexported fields
}
LineWriter calls a function with each line written to it, without its newline; blank lines are skipped. It is safe for concurrent writers (a split's pg_restores share it), and a line is never split between two of them as long as each writes whole lines, as the tools do.
func (*LineWriter) Flush ¶
func (w *LineWriter) Flush()
Flush says what is left of an unfinished last line.
type Options ¶
type Options struct {
Format Format
// Out is the file, or for directory and split the directory, to write.
// "" is stdout, for the single-file formats.
Out string
// Jobs dumps that many tables at once (directory and split only).
// pg_dump's workers share one snapshot, so the dump stays consistent.
Jobs int
SchemaOnly, DataOnly bool
// The filters take pg_dump's patterns: "public", "sales.*", "*log*".
Schemas, ExcludeSchemas []string
Tables, ExcludeTables, ExcludeData []string
Clean, IfExists, Create bool
NoOwner, NoPrivileges bool
Inserts, ColumnInserts bool
// Compress is pg_dump's --compress: a level ("6") or, from
// PostgreSQL 16, method[:detail] ("zstd:3", "lz4").
Compress string
Encoding string
// KeepArchive keeps, for split, the directory-format archive the SQL
// files were made from, as Out/archive — restorable with pg_restore.
KeepArchive bool
// Extra are pg_dump options dbc has no flag for, passed after its own:
// `dbc dump -- --no-comments --lock-wait-timeout=10s`.
Extra []string
}
Options are what to dump and how. The fields mirror pg_dump's options of the same names; the comments say only where dbc's meaning differs.
type Run ¶
type Run struct {
// Name is the dbc connection, for messages; ServerMajor its server's
// version, as Prepare found it (0 when not asked).
Name string
ServerMajor int
Tools Tools
Conn db.LibpqConn
Opts Options
// Stdout receives the dump when it goes to stdout (no Opts.Out).
Stdout io.Writer
// Stderr receives the tools' own messages (warnings, errors).
Stderr io.Writer
// Progress, if set, is told what is happening, a line at a time.
Progress func(string)
// contains filtered or unexported fields
}
Run is one dump.
func Prepare ¶
func Prepare(ctx context.Context, cfg *config.Config, mgr *db.Manager, conn string, opts Options) (*Run, error)
Prepare readies a dump of conn: the options checked, the connection restated, the server asked its version, a pg_dump new enough found — or the server's image in Docker when none is (LocateOrDocker). The UIs call it as the dialog's Dump is pressed, so whatever is wrong is said in the dialog rather than later, in a log.
func (*Run) Commands ¶
Commands are the command lines Do would run, for --dry-run. For split that is the archive's pg_dump and a description of the conversions, whose list of tables is only known once the archive exists.
func (*Run) Do ¶
Do runs the dump. Canceling ctx interrupts the tools (SIGINT, as Ctrl+C would), which end their server sessions.
func (*Run) Report ¶
Report runs r for a UI, saying each step, each line of the tools' own output, and the outcome through say(level, text) — level "info", "warn", "err" or "ok", the log levels both UIs draw. The error is returned too, for a UI that wants to tell a cancel from a failure.
func (*Run) RestoreNote ¶
RestoreNote is what the restore hint should add for a dump pg_dump ran in Docker: an archive (custom, tar, directory) is in the server's version's format, which an older pg_restore — this machine's, or it would have dumped — cannot read. "" otherwise.
type Tools ¶
type Tools struct {
Dump string // pg_dump's path
Restore string // pg_restore's, beside it; "" if not there
Version string // "17.2", as pg_dump reports it
Major int
// Image, when set, is the PostgreSQL image the tools run in, through
// Docker (docker.go): no local pg_dump was new enough for the server.
// Dump and Restore are then the tools' names in it, Docker the docker
// binary.
Image string
Docker string
}
Tools are the PostgreSQL client programs a dump runs.
func Locate ¶
Locate picks the pg_dump to run: from binDir when one is given, else the first on PATH, else from probeDirs.
pg_dump refuses a server of a newer major version than its own ("aborting because of server version mismatch"), and a machine often has several — an old one on PATH, a newer keg from Homebrew. So with serverMajor known (> 0) every candidate is asked its version, and the first that is new enough wins; a newer pg_dump reads an older server fine. needRestore skips a directory without pg_restore (the split format runs both).
func LocateOrDocker ¶
func LocateOrDocker(ctx context.Context, binDir string, serverMajor int, needRestore bool) (Tools, error)
LocateOrDocker is Locate, falling back on the server's image in Docker when no local tools are new enough (see pg_dump FROM DOCKER). Only when the tools were looked for (no binDir named: one given is meant) and the server's major is known; Locate's own error stands when Docker cannot help, so it is the message the user sees.