Instantly share code, notes, and snippets.

Embed
What would you like to do?
Easily start a local Postgres instance for your development environment using docker
DATA_DIR="__data"
POSTGRES_VERSION=9.3
PORT=5432
.PHONY: docker-check postgres
docker-check:
@command -v docker >/dev/null 2>&1 || \
{ echo >&2 "Docker needs to be installed and on your PATH. Aborting."; exit 1; }
postgres: docker-check
@if nmap -PS localhost | grep -q $(PORT); then \
echo "ERROR: Port $(PORT) is already in use..."; \
echo "Maybe Postgres is already running?!"; \
exit 1; \
fi
@if [ ! -d $(DATA_DIR)/postgresql ]; then \
echo 'Preparing Postgres persistent data storage...'; \
mkdir -p $(DATA_DIR); \
docker run -v $$PWD/$(DATA_DIR):/tmp/$(DATA_DIR) -i -t \
denibertovic/postgres:$(POSTGRES_VERSION)\
/bin/bash -c "cp -rp var/lib/postgresql /tmp/$(DATA_DIR)"; \
fi
@echo "Persistent data storage found.";
@echo "Starting postgres...";
@docker run -v $$PWD/$(DATA_DIR)/postgresql:/var/lib/postgresql -d -p $(PORT):$(PORT) \
denibertovic/postgres:$(POSTGRES_VERSION);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment