How one simple file transformed my development workflow from 10 minutes to 30 seconds
As a developer, I was constantly typing the same Docker and database commands over and over. Starting PostgreSQL containers, running migrations, creating databases - it was getting repetitive and error-prone.
Setting up my local development environment was taking forever every time I switched between projects or started fresh. I needed something to make local development setup quick and consistent.
That's when I discovered I could use Makefiles to automate all these database operations for faster local development. Here's what I learned.
What is a Makefile?
A Makefile is a simple text file that contains commands you can run with the make
command. While most people think it's only for compiling C programs, it's actually great for automating any repetitive tasks - including database operations.
My Database Makefile for Local Development
Here's the Makefile I created to speed up my local development setup with PostgreSQL:
# Default target - shows help when you just run 'make'
.DEFAULT_GOAL := help
help: ## Show all available commands
@echo "Available commands:"
@grep -E '^[a-zA-Z_-]+:.*?## .*$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf " %-15s %s\n", $1, $2}'
postgres: ## Start PostgreSQL container
docker run --name postgres -p 5432:5432 -e POSTGRES_USER=root -e POSTGRES_PASSWORD=secret -d postgres
remove-postgres: ## Stop and remove PostgreSQL container
docker stop postgres
docker rm postgres
createdb: ## Create database
docker exec -it postgres createdb --username=root --owner=root simple_bank
dropdb: ## Delete database
docker exec -it postgres dropdb simple_bank
migrateup: ## Run database migrations
migrate -path db/migration -database "postgresql://root:secret@localhost:5432/simple_bank?sslmode=disable" up
migratedown: ## Rollback database migrations
migrate -path db/migration -database "postgresql://root:secret@localhost:5432/simple_bank?sslmode=disable" down
.PHONY: help postgres remove-postgres createdb dropdb migrateup migratedown
How Each Command Works
Starting PostgreSQL
make postgres
This starts a PostgreSQL container with:
- Container name:
postgres
- Port:
5432
- Username:
root
- Password:
secret
Stopping PostgreSQL
make remove-postgres
This stops and removes the PostgreSQL container completely.
Creating Database
make createdb
This creates a new database called simple_bank
inside the running PostgreSQL container.
Dropping Database
make dropdb
This deletes the simple_bank
database.
Running Migrations
make migrateup
This runs all pending database migrations from the db/migration
folder.
Rolling Back Migrations
make migratedown
This rolls back the last migration.
Why Use Makefile for Local Development?
Before Makefile (slow setup):
# I had to remember and type these long commands every time
docker run --name postgres -p 5432:5432 -e POSTGRES_USER=root -e POSTGRES_PASSWORD=secret -d postgres
docker exec -it postgres createdb --username=root --owner=root simple_bank
migrate -path db/migration -database "postgresql://root:secret@localhost:5432/simple_bank?sslmode=disable" up
After Makefile (quick setup):
# Simple, fast commands for local development
make postgres
make createdb
make migrateup
Now I can get my local development environment running in seconds instead of minutes!
Adding a Help Command
One really useful feature I added is a help command that shows all available commands:
make help
This displays:
Available commands:
createdb Create database
dropdb Delete database
help Show all available commands
migratedown Rollback database migrations
migrateup Run database migrations
postgres Start PostgreSQL container
remove-postgres Stop and remove PostgreSQL container
You can also just run make
without any command, and it will show the help automatically.
The help command works by:
- Adding
## description
after each command - Using a special help target that parses these descriptions
Setting
.DEFAULT_GOAL := help
somake
alone shows helpCreate a file named
Makefile
in your project root-
Copy the commands above and modify them for your project:
- Change database name from
simple_bank
to your database name - Update username/password if needed
- Adjust the migration path if different
- Change database name from
Run commands using
make <command-name>
Important Notes
- The indentation in Makefile must be tabs, not spaces
- The
.PHONY
line tells Make that these aren't file names - Make sure Docker is running before using these commands
- Install
migrate
tool if you want to use migration commands
Common Local Development Workflow
Here's how I typically use these commands to set up my local development environment:
# Quick local setup when starting work
make postgres
make createdb
make migrateup
# Now I can start coding immediately!
# When switching projects or need fresh database
make remove-postgres
make postgres
make createdb
make migrateup
# At end of day (optional - frees up resources)
make remove-postgres
This workflow gets me from zero to coding in under 30 seconds!
Benefits for Local Development
- Super fast setup - From zero to coding in 30 seconds
- No more typos - I don't have to type long Docker commands
- Consistency - Same setup works on any machine
- Easy project switching - Quick tear down and setup
- Team onboarding - New developers can start immediately
Conclusion
Using Makefiles for local development database operations has been a game-changer for my workflow. Instead of memorizing complex Docker and migration commands, I just run simple make
commands and get my development environment ready instantly.
If you're tired of slow local development setup and want to start coding faster, give this approach a try. Start with basic commands like the ones I shared, and you'll quickly see how much time it saves in your daily development routine.
Quick Reference
make postgres # Start PostgreSQL container
make remove-postgres # Stop and remove container
make createdb # Create database
make dropdb # Delete database
make migrateup # Run migrations
make migratedown # Rollback migrations
Top comments (0)