Applications and operations

Back up and restore PostgreSQL

Create a custom-format dump and verify it by restoring a separate database.

8 minute lesson

~~~

Create a custom-format archive:

pg_dump --format=custom --file=notes.dump notes_app

Restore it into a clean database, never over the source:

createdb --template=template0 notes_restore
pg_restore --exit-on-error --no-owner \
  --dbname=notes_restore notes.dump

Verify the restored data:

psql notes_restore -c 'SELECT count(*) FROM app.notes'

pg_dump backs up one database. Roles and other cluster-global objects are outside it. Back them up separately when you operate the cluster:

pg_dumpall --globals-only > globals.sql

Inspect and protect that file. It contains role definitions and can contain password hashes. A successful dump job is not enough: restore it and run the application against the result.

Lesson completed

Take this course offline

Get every free book and course as PDF and EPUB files.

Get the download library →