Backup and Restore in PostgreSQL

PostgreSQL is a object-relational database management system. It is most advanced open soruce relational database.

Taking backups using pg_dump :

pg_dump is a utility for backing up a PostgreSQL database.

We can extract the database in script file or archive file formats :

Examples :

$ pg_dump {DATABASENAME} > {FILENAME}.sql
$ pg_dump -Fc {DATABASENAME} > {FILENAME}.dump
$ pg_dump -Fc {DATABASENAME} > {FILENAME}.zip
$ pg_dump -h {HOSTNAME} -U {USERNAME} {DATABASENAME} > {FILENAME}.sql

$ pg_dump -t {TABLENAME} {DATABASENAME} > {FILENAME}.sql

Restore database using pg_restore :

pg_restore is a utility for restoring a PostgreSQL database from an archive created by pg_dump (in one of the non-plain-text formats).

Examples :

$ pg_restore -d {NEWDATABASE} {FILENAME}.dump
$ pg_restore -d {NEWDATABASE} {FILENAME}.zip
$ pg_restore -h {HOSTNAME} -U {USERNAME} -d {NEWDATABASE} {FILENAME}.dump

Restore database using psql :

Psql is the interactive terminal for working with Postgres.

Examples :

$  psql -d {NEWDATABASE} -f {FILENAME}.zip
$  psql -h {HOSTNAME} -U {USERNAME} -d {NEWDATABASE} -f {FILENAME}.zip

Comments

comments powered by Disqus