A local database keeps growing after you stop using the project
Postgres, MySQL and MongoDB data directories persist long after the app that needed them. Where they are and how to remove one safely.
A local database installed for one project keeps running, keeps its data, and keeps its write ahead logs, long after the project is finished. Nothing about uninstalling the client or deleting the repository touches any of it.
Where each one puts its data
du -sh /opt/homebrew/var/postgres* /opt/homebrew/var/mysql \
~/Library/Application\ Support/Postgres \
/opt/homebrew/var/mongodb 2>/dev/null | sort -hHomebrew keeps data under its own var directory, and the app based installers keep it in Application Support. A Mac that has used both has two sets, and only one of them is running.
Look before removing
brew services list
psql -l 2>/dev/null
mysql -e 'show databases;' 2>/dev/nullThose list what is actually there. A database you cannot identify from its name is worth dumping before removing, which takes a minute and turns an irreversible decision into a reversible one:
pg_dump mydb > ~/Desktop/mydb.sql
mysqldump mydb > ~/Desktop/mydb.sqlThe parts that grow on their own
- Write ahead logs. Postgres keeps WAL segments; a database left running for months accumulates them.
- Binary logs. MySQL keeps these for replication, and they are not needed on a development machine.
- Dropped tables. Space is not returned to the file system without a vacuum full or an optimise.
- Old major versions. Upgrading Postgres creates a new data directory and leaves the previous one in place.
Removing a database you have finished with
- Dump anything you might want, to a file you keep somewhere sensible.
- Stop the service with
brew services stop. - Remove the data directory for that version.
- Uninstall the formula if nothing else uses it.
Old major version directories are the easiest win here, and they are the same pattern as Docker volumes: data that outlives the thing that created it and that nothing will ever clean up for you.
Common questions
Where does Homebrew install Postgres data on a Mac?
Under /opt/homebrew/var, in a directory named for the major version. Upgrading creates a new directory and leaves the old one in place, which is why several can accumulate.
Is it safe to delete a local database data directory?
Only after dumping anything you want to keep and stopping the service. The directory is the database, so removing it is permanent for that data.
Why does a local database keep growing?
Write ahead and binary logs accumulate, and dropped tables do not return space to the file system without a vacuum or optimise. A service left running for months grows even when unused.
How do I see which databases exist on my Mac?
Run brew services list to see what is running, then psql -l or mysql -e 'show databases;' to list the databases each server holds.