Dump & Restore PostgreSQL database in a Docker
I was updating my blog and needed to use the same database which I currently have in production. Since my blog is fully dockerized, managing the state of the application between production & development environment is a lot easier. For this purpose, I have researched and learned two handy commands, one to export the database and the other one to import the database. Export (dump) with pg_dump{.single}: 1 docker exec -i container_name /bin/bash -c “PGPASSWORD=actual_password pg_dump —username actual_username database_name” > /path/of/export/export.sql Import (restore) with psql{.single}: 1 docker exec -i container_name /bin/bash -c “PGPASSWORD=actual_password psql —username actual_username database_name” > /path/of/export/export.sql I have noticed successful import even with errors (default PostgreSQL behaviour), because I already had some migrations (in my Django app). Therefore I would recommend first resetting the whole DB and then importing the database. If you want to overwrite this default behaviour and stop upon error, add -set ON_ERROR_STOP=on{.single} to your command. ...