How To Duplicate a PGSQL database
August 10, 2009
Recently I needed to create a duplicate of PGSQL database. Being a MySQL guy, I didn’t know off hand how to do it, but here is an easy way:
pg_dump original_db > original_db.sql
createdb new_db
psql -d new_db < original_db.sql
From a bird’s eye view, you are creating a dump of the original database and outputting it to a file. From there, make sure the new database is created and use your dump file as input for that database. It wouldn’t be too hard to make this into a one-liner, but I like to see intermediate steps to make sure things are going well.