Dump and restore commands for PostgreSQL in Docker

Sometimes during development it’s necessary to share the db dump with colleagues. In the previous article i provided the commands for MySQL, here is the same for PostgreSQL.

DATE = $(shell date +%Y-%m-%d)
path ?= .

help: ## show this help
	@echo 'usage: make [target] ...'
	@echo ''
	@echo 'targets:'
	@egrep '^(.+)\:\ .*##\ (.+)' ${MAKEFILE_LIST} | sed 's/:.*##/#/' | column -t -c 2 -s '#'

dump: ## dump db, usage 'make dump path=/path/to/dumps'
	docker-compose exec --user postgres db pg_dumpall --clean | gzip > $(path)/project-$(DATE).sql.gz

restore: ## restore db from dump file, usage 'make restore dump=dump.sql.gz'
	docker-compose stop web
	gunzip -c $(dump) | docker exec -i --user postgres `docker-compose ps -q db` psql
	docker-compose start web
comments powered by Disqus