Every now and then I need to restore an old database backup to grab some data out of. I use Postgres containers for every app I work on, so I don’t really want to go polluting those. Here’s how I start a postgres container, restore a DB into it, do what I need to do, then destroy it.
docker run --rm -it -v `pwd`:/data postgres bash
--rmmeans remove the container when it stops (we exit bash)
-itmeans give me a interactive terminal (I need to type stuff and see output)
-vmeans mount the directory I’m currently in to
/data- assuming the directory I’m in has a PG dump I want to use.
postgrescontainer has an entrypoint script that normally does this for us, but we’ve told Docker to run
bashrather than this script.
pg_createcluster 9.5 main(replace 9.5 with whatever version of Postgres you have used)
service postgresql start
su - postgres -c "createuser -s root"
psql my_database < my_database_backup.sql
SELECT count(*) from widgets