My Postgres setup notes start with brew install postgresql@17 and brew services start postgresql@17. That gives you one Postgres, always running, with psql on your path. It served me well until the day one project wanted Postgres 16, another wanted 17, and a third needed an extension the Homebrew build didn’t ship. That’s the day Docker starts making sense.
What Homebrew gives you
One native Postgres for the whole machine. It’s fast, it starts at login, and every project connects to the same server and gets its own database with createdb. Configuration lives in one place, /opt/homebrew/var/postgresql@17, and upgrades are brew upgrade plus a pg_upgrade you will put off for months.
The weaknesses are the flip side of “one for everything”: one version, one set of settings, one global cluster that slowly accumulates databases from projects you’ve forgotten. And nothing about it is written down in the project, so a teammate on Linux gets to reinvent it.
What Docker gives you
Postgres as an appliance per project, described in a file that lives in the repo. This is the whole thing:
services:
db:
image: postgres:17
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: app
POSTGRES_DB: app
ports:
- "5432:5432"
volumes:
- pgdata:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U app -d app"]
interval: 5s
timeout: 3s
retries: 10
volumes:
pgdata:Save it as compose.yaml, run docker compose up -d, and you have Postgres 17 listening on port 5432 with a user, a password, and a database called app. In plain English, block by block:
image: postgres:17 is a frozen, complete install of Postgres 17, downloaded once. Pin the major version. postgres:latest will one day become 18, and Postgres 18 will refuse to start on a data directory written by 17, which is a confusing morning.
environment is the first-boot setup. The official image creates that user and database the first time it starts with an empty data directory, and never again. Change the password here after that first boot and nothing happens; the database already exists.
ports maps your machine’s port to the container’s. The left number is yours. Two projects at once means "5433:5432" on the second one and a different port in its connection string.
healthcheck teaches Docker how to tell “the process started” from “Postgres is accepting connections”, which is what an app service needs to wait for (depends_on with condition: service_healthy).
volumes is the part that matters.
What a volume actually is
A container is disposable. Stop it, remove it, start a new one from the image, and you’re back to a fresh install. That’s the feature. It’s also how you lose a database.
A named volume is a folder that Docker keeps outside the container and mounts inside it at a path you choose. Here pgdata is mounted at /var/lib/postgresql/data, which is where Postgres writes everything. The container can come and go; the folder stays.
Two commands, one letter apart:
docker compose down # removes the container. The volume, and your data, stay.
docker compose down -v # removes the container AND the volume. Your data is gone.up after a plain down finds the existing volume, skips the first-boot setup, and your tables are exactly where you left them. -v is for when you want a clean slate.
Connecting to it
The connection string is the same as it would be for any Postgres:
postgresql://app:app@localhost:5432/appYou don’t need Postgres installed on your machine to talk to it. brew install libpq gives you psql on its own, or skip that and use the one inside the container:
docker compose exec db psql -U app -d appThe gotchas, in the order I hit them
Port 5432 is already taken, because Homebrew’s Postgres is still running. Either brew services stop postgresql@17 or change the left-hand port in compose.yaml.
The password didn’t change. See environment above: it only applies to an empty data directory. down -v and up if it’s a dev database you don’t care about.
The container is “running” but the app can’t connect yet. Postgres takes a second or two to be ready; that’s what the healthcheck is for.
Someone bumped the image to a new major version and Postgres won’t start. The data directory is from the old version. Go back to the old tag, dump with pg_dump, then move to the new version with an empty volume and restore.
Which one, then
Both. Homebrew’s Postgres is still my scratch instance, the one I open psql against to try a query or reproduce something for a blog post, and it’s what gives me psql at all. Anything with a repo gets a compose.yaml, so the database version and setup are part of the project and the next machine, or the next person, gets the same thing with one command.
If you only want one: pick Docker, install libpq for psql, and pin your major version.