Postgres provides us several options to do backup just like we want. Iβve tried several strategies in the past, and here is the one I stick to now, for both its simplicity and efficiency.
Backup your database with:
# This is using Postgres custom format
pg_dump -Fc dbname > filename
And recover it as follow:
# First create the DB with:
createdb -O owner_user dbname
# And then populate it:
pg_restore -d dbname filename