PostgreSQL database backup and restore
Sunday, February 24th, 2008In the last couple of weeks I have been using PostgreSQL quite a lot. I have used PostgreSQL before but this time it was different as the development as well as the deployment environment were Linux based, as a result I was denied of the use of pgAdminIII, the GUI based administration tool for PostgreSQL database. My only option was to use the command prompt.
The need of the hour was to backup the live database and restore it on the test environment, which incidentally was also a Linux box. With pgAdminIII, it would have been really simple, take a backup of the database in a binary format and restore the test database from the binary file. So, off I went, digging into the PostgreSQL documentation looking for the commands which will do the same for me and wolla, I found pg_dump and pg_restore.
pg_dump is the utility for backing up a PostgreSQL database. Following is the command with it’s options that will create a binary backup of an entire database;
pg_dump -i -h host-name -p port-number -U username -F c -b -v -f "backup-filename.backup" dbname
pg_restore is the utility for restoring a PostgreSQL database from an archive created by pg_dump in one of the non-plain-text formats. Following is the command with it’s options that will restore the database from the binary backup file;
pg_restore -i -h host-name -p port-number -U username -d dbname -a -v "backup-filename.backup"
I noticed an interesting point while using pg_restore. pg_restore doesn’t work properly if there are referential integrity among the tables on the target database as tables are restored sequentially. So, the best restoring approach is to first create the tables in the database without enforcing their referential integrity, then run pg_restore to perform the magic. Once restoring is complete you can easily run another script to enforce all the referential integrities at one go.
On a completely different note, it is always considered as a best practice to test your application with a real data-set. This will give you the opportunity to write off those final few bugs which are only exposed when the application is used with a real set of data. What else could be better than the replica of the live database! More over, now you can backup and restore entire PostgreSQL database, even without the help of pgAdminIII.