How To Duplicate a PGSQL database

Tim Barsness

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.

Last updated by on .

28 thoughts on "How To Duplicate a PGSQL database"

  1. Steve says:

    Hey, just wanted to let you know that this worked really well. For how much this has to be searched on Google, this little gem of three commands was a needle in a haystack. Couldn’t find it anywhere! Thanks for the help. Cheers!

    ~Steve

  2. Tim Barsness says:

    Thanks Steve. I ran across a need for this as well and it took me a while to figure out a solution, so I figured I would share it.

What Are Your Thoughts?

Your email address will not be published. Required fields are marked *