PostgreSQL client wrapper around psql, pg_dump, and pg_restore.
Routes everything through Trebejo.Util.run_cmd/3 (and therefore
Trebejo.Runner) so it picks up :timeout, the circuit breaker, and
Trebejo.Mox mocking automatically.
All connection parameters are passed via env vars (PGHOST, PGUSER,
PGPASSWORD, PGDATABASE, PGPORT) and command-line flags. Passwords
are never interpolated into shell strings — use the env var or a
~/.pgpass file.
Options (shared)
:host—PGHOST, defaultlocalhost:port—PGPORT, default5432:user—PGUSER, default$USER:database—PGDATABASE:password— passed asPGPASSWORDenv var (NOT as-W):timeout— passed through to the underlying runner:breaker— circuit breaker name (passed toTrebejo.Breaker.with_breaker/3)
Examples
iex> Trebejo.Postgres.execute("SELECT 1", host: "db.internal", database: "app")
{:ok, "1\n", 0}
iex> Trebejo.Postgres.dump("app", file: "/tmp/app.dump", host: "db.internal")
:ok
Summary
Functions
Dump a database to a file using pg_dump.
Run a SQL statement or query via psql -c.
Restore a dump produced by pg_dump (or pg_dumpall).
Stream a query via psql line by line. See Trebejo.Stream.lines/3.
Functions
@spec dump( String.t(), keyword() ) :: :ok | {:error, Trebejo.Error.t()}
Dump a database to a file using pg_dump.
If :format is :plain (default) the file is a SQL script.
Otherwise it is a custom-format dump suitable for pg_restore.
@spec execute( String.t(), keyword() ) :: {:ok, String.t(), 0} | {:error, Trebejo.Error.t()}
Run a SQL statement or query via psql -c.
Returns {:ok, stdout, 0} on success or {:error, %Trebejo.Error{}}.
@spec restore( String.t(), keyword() ) :: :ok | {:error, Trebejo.Error.t()}
Restore a dump produced by pg_dump (or pg_dumpall).
Pass :format: :plain for SQL dumps and any other value for custom
dumps.
@spec stream( String.t(), keyword() ) :: {:ok, Enumerable.t()} | {:error, Trebejo.Error.t()}
Stream a query via psql line by line. See Trebejo.Stream.lines/3.