Skip to content

Back up PostgreSQL as .dump and restore it with pg_restore - #41

Merged
necessarylion merged 3 commits into
mainfrom
pg-restore
Nov 8, 2026
Merged

necessarylion merged 3 commits into
mainfrom
pg-restore

Conversation

@necessarylion

Copy link
Copy Markdown
Owner

Why

The SQL import is slow on big PostgreSQL databases, and some files still fail. PostgreSQL has its own tools for this: pg_dump --format=custom and pg_restore. This PR uses them, like TablePlus does.

What

  • Backup format (PostgreSQL only). The Back up button opens a small dialog: "Plain SQL (.sql)" (as before) or "Custom (.dump, for pg_restore)". The save dialog uses the matching file type. Progress, cancel and the .part side file work for both.
  • Tool version. The dialog lists the pg_dump / pg_restore versions on this computer ("PostgreSQL 17", "PostgreSQL 15", ...). It reads each tool's real version with --version. The default is the server version (SHOW server_version_num), else the newest one. A tool older than the server shows a warning.
  • Restore from .dump… in the Import menu. Pick a file, the version and "Drop objects first (--clean --if-exists)". It asks to confirm and names the database. It runs pg_restore --no-owner --no-privileges into the current database. The password goes in PGPASSWORD, never in the arguments. Progress shows the time and the table. Cancel kills the tool. pg_restore goes on after errors, so the error says how many there were and shows the real error lines.
  • SQL import of a file that starts with PGDMP goes to the restore dialog. The backend refuses it as SQL with a clear message.
  • MySQL, SQLite and SQL Server do not change.

Not done

  • The last chosen version is not remembered per connection. The default (server version) is usually right.

Checks

  • cargo clippy --all-targets (zero warnings), cargo test, cargo fmt
  • PostgreSQL end-to-end ran against Docker PostgreSQL 17: custom backup, pg_restore into a new database, check the rows, restore again (errors are counted), restore with clean, drop. Found pg_dump versions [17, 15].
  • bun run lint, bun run typecheck, bun test (393 pass)

The plain SQL import is slow on big PostgreSQL databases. Now the backup can
write a custom format .dump file, and a new restore loads it with pg_restore.

- Backup dialog for PostgreSQL: pick plain SQL or custom format, and the
  pg_dump version. The default is the version of the server.
- Restore from .dump: pick the pg_restore version and whether to drop
  objects first. It shows progress, can be cancelled, and counts errors.
- The SQL import sends a PGDMP file to the restore dialog.
fn the_restore_password_is_never_an_argument() {
let file = Path::new("/backups/-shop.dump");
let tool = Path::new("pg_restore");
let command = DumpRepository::restore_command(tool, &postgres("s3cret"), file, true);
.and_then(|(_, value)| value);
assert_eq!(password, Some(OsStr::new("s3cret")));
// Without clean and without a password.
let command = DumpRepository::restore_command(tool, &postgres(""), file, false);
- Pass the database name to pg_dump and pg_restore as dbname='...'. A name
  with = or postgresql:// can then never be read as a connection string.
- Ask to confirm a PostgreSQL backup, like MySQL and SQLite do.
- Keep the pg_restore error when the reload of the tables fails after it.
- A PGDMP file on MySQL or SQLite says it needs a PostgreSQL database.
- Also find the PostgreSQL tools of Intel Homebrew and RHEL.
A fatal pg_restore error, like a wrong password or a file from a newer
pg_dump, is also an error line, but then nothing was restored. Now the count
comes from the "errors ignored on restore: N" line, which pg_restore prints
only when it went on. Without that line the restore shows just its errors.
@necessarylion
necessarylion merged commit f23bd80 into main Nov 8, 2026
4 of 5 checks passed
@necessarylion
necessarylion deleted the pg-restore branch November 8, 2026 01:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants