Taking database dumps

We primarily use 2 databases

  • PostgreSQL
  • MongoDB

We'll cover ways to import/export dumps for both databases.

PostgreSQL

  • Have pg_dump and psql installed. Check the overview guide to install the postgres cli tools.
  • Let's say our database is called superapp. To take a db dump,
$ cd /tmp
$ pg_dump -U postgres -h localhost superapp > superapp_20220824.sql # putting a date is a good idea :)
$ zip superapp_20220824.sql.zip superapp_20220824.sql
  • You can then send the /tmp/superapp_20220824.sql.zip archive to who ever that needs it.
  • To import a dump
$ cd /tmp
$ cp ~/Downloads/superapp_20220824.sql.zip /tmp/superapp_20220824.sql.zip
$ unzip superapp_20220824.sql.zip
$ psql -U postgres -h localhost superapp < superapp_20220824.sql
$ rm superapp_20220824.sql.zip
  • Moving to /tmp while doing this is optional. This is weird thing this author does :)

MongoDB

  • Have mongodump and mongorestore installed. Check the overview guide to install the mongo cli tools.
  • Let's say our database is called simpleapp. To take a db dump,
$ cd /tmp
$ mongodump --db simpleapp
$ tar -cvf simpleapp.20220824.tar.gz dump
  • You can then send the /tmp/simpleapp.20220824.tar.gz archive to who ever that needs it.
  • To import a dump
$ cd /tmp
$ cp ~/Downloads/simpleapp.20220824.tar.gz /tmp/simpleapp.20220824.tar.gz
$ tar -xvf simpleapp.20220824.tar.gz
# you should see a folder named dump. This *needs* to be here!
$ mongorestore
$ rm simpleapp.20220824.tar.gz
$ rm -rf dump