Back up PostgreSQL over SSH to your Mac: pg_dump by hand and on schedule
pg_dump dumps the database on the server, and the compressed stream goes over SSH straight into a file on your Mac. Nothing stays on the server, and all it needs there is pg_dump itself.
One command
ssh deploy@example.com "set -o pipefail; pg_dump -h localhost -U shop shop | gzip" \
> pgsql-shop-$(date +%F).sql.gz
pg_dump and gzip run on the server, and the Mac receives an already compressed dump. set -o pipefail keeps a pg_dump error from getting lost: without it the command returns the exit code of gzip, which almost always succeeds. If the server has zstd, replace gzip with zstd -T0 for a faster run and a smaller file.
If the database lets the system user postgres in without a password, as a stock install on Ubuntu or Debian does, you need no password at all:
ssh deploy@example.com "set -o pipefail; sudo -u postgres pg_dump shop | gzip" > pgsql-shop.sql.gz
A password off the command line
A password in PGPASSWORD or in a connection URL easily leaks into shell history and the process list. Put it in ~/.pgpass on the server instead:
localhost:5432:shop:shop:your-password
The format is host:port:database:user:password, and any field can be *. Lock the file down with chmod 600 ~/.pgpass, or PostgreSQL ignores it.
The pg_dump version
pg_dump must be the same version as the PostgreSQL server or newer. Otherwise it stops with server version mismatch. Check both versions like this:
ssh deploy@example.com "pg_dump --version; psql -h localhost -U shop -d shop -Atc 'show server_version'"
If the client is older, install the matching postgresql-client package on the server from the PostgreSQL repository.
Which format
- Plain SQL (the default,
-Fp) is human-readable, compresses with anything and restores withpsql. - Custom (
-Fc) is compressed already and restores withpg_restore. Its advantage is that you can pull a single table out of the dump.
For a small project's backups plain SQL is enough: the file opens on any computer with psql.
If the database will be restored on a different server, add --no-owner --no-privileges. Then the dump doesn't need the same roles the original server had.
Check the dump is complete
A complete plain-format dump ends with the line -- PostgreSQL database dump complete. If it's missing, the dump was cut off:
gzip -t pgsql-shop-2026-10-18.sql.gz
gunzip -c pgsql-shop-2026-10-18.sql.gz | tail -n 8 | grep "dump complete"
How to restore
createdb shop_test
gunzip -c pgsql-shop-2026-10-18.sql.gz | psql -X -1 -v ON_ERROR_STOP=1 -d shop_test
-1 loads the dump in a single transaction, and ON_ERROR_STOP stops it at the first error. If something goes wrong, the database stays empty instead of half-filled. On a Mac, psql and createdb come with brew install libpq. It's even easier to restore the dump in Docker, see "How to test a database backup".
Roles and server settings
pg_dump saves one database. Users and their privileges live at the server level, and a separate command saves them:
ssh deploy@example.com "sudo -u postgres pg_dumpall --globals-only" > pg-globals.sql