Saturday, September 25, 2010

Backup and Restore Postgres DB

pg_dump --create --format=t --file=mydb.tar mydb

pg_restore --clean --format=t --dbname=mydb mydb.tar


[2011-01-17]

Maybe a better idea is to use the plain SQL export format, as this gives you an option to edit the output file and e.g. to change the text format from LATIN1 to UTF8 later on. It is also easy to change the name of the database before recreating it! Last not least, the -d parameter uses insert instead of copy statements. This is much slower when recreating the database, but unlike with copy, this once worked successfully for me when changing the encoding at the same time.

pg_dump --create -d --format=p --file=mydb.sql mydb

#emacs mydb.sql
createdb -E UTF8 mydb
pg_restore -d mydb -f mydb.sql