Postgres backup and recovery

April 20, 2015 | Posted in PostgreSQL | Ajoy Oommen

It is very easy to backup a postgres database and restore it. The commands used are pg_dump and pg_restore. A backup can be created in 4 formats:

  1. Plain SQL script file
  2. Custom-format
  3. Directory-format
  4. Tar-format archive

I usually prefer the custom-format as it “is the most flexible output format” and “is also compressed by default”. The commands are:

pg_dump -U postgres -W dbname -Fc -f filename.db.backup

This file can be restore to a new database with:

pg_restore -U postgres -Fc -d dbname filename.db.backup
backup PostgreSQL

Related Posts

27 Feb 2017 » Provisioning an ubuntu server for Django, Postgres, NGINX

11 Jul 2016 » Installing PostgreSQL

02 Apr 2015 » PostgreSQL module ltree

04 Sep 2014 » Forwarding port to a PostgreSQL server