Copying a postgres database from one server to another
If you want to take a database mydb
on server
and copy it to mydb
on server2
, completely replacing all contents of mydb
on server2
, dump with something like:
pg_dump -Fc -f mydb.dump -h server1 mydb
then restore with:
dropdb -h server2 mydb
createdb -h server2 -T template0 mydb
pg_restore -d mydb -h server2 mydb.dump
This will:
DROP
databasemydb
onserver2
, completely and permanently destroying all data inmydb
onserver2
- Re-
CREATE
databasemydb
onserver2
from a totally empty template - Restore the copy of
mydb
onserver
intoserver2
Another option is to use pg_restore --clean
without the drop and create. That'll drop all tables then re-create them. I prefer to drop the whole DB and get a clean one instead.