Last Updated: February 25, 2016
· dejnon

Create a snapshot of postgres database

When writing migrations I sometimes damage my database, also when working on different sub-projects and you want to have a DB in sync with production and without migrations from project you are not working on currently.

First, list your databases in psql:

psql -U <user> -l

Then do a copy of the one you are interested in:

createdb -O <user> -T <db_to_be_cloned> <clone_name>

Then if something goes wrong you first delete the DB that got damaged:

dropdb <db_to_be_deleted>

And do the createdb again.

You may also be interested in renaming the db:

psql -U <user>
ALTER DATABASE <old_name> RENAME TO <new_name>


Say Thanks