Port 5432 already in use on Mac? Fix Postgres
Postgres won’t start because port 5432 is taken, or Docker can’t publish it. On a Mac that nearly always means a second Postgres: one from Homebrew, Postgres.app, Docker or the official installer. Find which, then stop it the right way so it doesn’t come back.
By Anmol Sethi · Updated
In short
Run sudo lsof -nP -iTCP:5432 -sTCP:LISTEN (the sudo matters: the installer’s Postgres runs as another user). Then stop it with the tool that started it: brew services stop postgresql@17 for Homebrew, Stop in Postgres.app, docker stop <name> for a container. Killing the process alone doesn’t work, because its service manager restarts it. Or keep both and run the second one on 5433.
Find what holds 5432
sudo lsof -nP -iTCP:5432 -sTCP:LISTENCOMMAND PID USER FD TYPE NODE NAME
postgres 812 you 7u IPv6 TCP [::1]:5432 (LISTEN)
postgres 812 you 8u IPv4 TCP 127.0.0.1:5432 (LISTEN)Without sudo you only see your own processes, so a Postgres running as the postgres user stays hidden and the port looks free. Then get the full command line, which says where it came from:
ps -o user=,command= -p <PID>| The command or owner contains | It’s | Stop it with |
|---|---|---|
/opt/homebrew/ or /usr/local/ (Cellar) | Homebrew | brew services stop postgresql@17 |
Postgres.app | Postgres.app | Stop in Postgres.app, then quit it |
com.docker, vpnkit or OrbStack | A Docker container | docker stop <container> |
/Library/PostgreSQL/ and user postgres | The EDB installer | launchctl bootout (below) |
DBngin | DBngin | Stop it in DBngin |
Homebrew
List the services to get the exact formula name, then stop that one:
brew services list
brew services stop postgresql@17stop also keeps it from starting at login. Several versions installed (postgresql@14, @16, @17)? Only one can have 5432, so stop the ones you don’t use. More in Homebrew services on Mac.
Docker
docker ps --filter publish=5432
docker stop <container>If Docker says Bind for 0.0.0.0:5432 failed: port is already allocated, it’s the other way round: a local Postgres already has the port and the container can’t take it. Stop the local one, or publish the container on another port: -p 5433:5432. See the Docker port guide.
The installer from postgresql.org (EDB)
This Postgres runs as a system service owned by the postgres user and starts with the Mac. Find its file, then stop it:
ls /Library/LaunchDaemons | grep -i postgres
sudo launchctl bootout system /Library/LaunchDaemons/<that-file>.plistIt starts again at the next restart. To remove it for good, run the uninstaller in /Library/PostgreSQL/<version>/.
Keep both: run the second one on 5433
Needing two versions side by side is normal. Give the second one its own port:
- Docker:
-p 5433:5432, then connect tolocalhost:5433. - Postgres.app: set the port in the server’s settings.
- Homebrew: set
port = 5433in that version’spostgresql.conf(brew info postgresql@17shows the data folder), thenbrew services restart postgresql@17.
Update the port in your app’s DATABASE_URL to match.
Connected, but to the wrong Postgres
With two servers installed, errors like role "postgres" does not exist or a missing database often mean you reached the other one. Ask the server which it is:
psql -h localhost -p 5432 -c "show data_directory;" -c "select version();"“Lock file postmaster.pid already exists”
Postgres was stopped uncleanly (a crash or a forced restart) and left its lock file behind. First make sure no Postgres is running with the lsof command above. If none is, delete postmaster.pid from the data folder and start it again. Never delete it while a server is running.
The one-click way: Portlivo
Portlivo shows Postgres from Homebrew, Postgres.app, DBngin or a Docker container as a named row in the menu bar, with its formula or container name. Stop uses brew services stop or docker stop, so it stays stopped instead of coming straight back.
Servers owned by other users, like the EDB installer’s, aren’t listed, since macOS doesn’t show them without sudo. Use the steps above for those.
Questions people ask
- Why does lsof show nothing on 5432 when Postgres says it’s in use?
- The server holding it runs as another user, usually
postgresfrom the official installer. Runsudo lsof -nP -iTCP:5432 -sTCP:LISTENto see it. - I killed postgres and it came back. Why?
- A service manager restarted it: Homebrew’s launchd agent, the installer’s system service or Docker. Stop it through that tool, such as
brew services stop, and it stays stopped. - Can I run two Postgres versions at once?
- Yes, on different ports. Keep one on 5432, move the other to 5433, and point each app’s connection string at the right one.
Try Portlivo free for 7 days. Every feature, no account.Then $8.99 once, no subscription.
Download for MacSend to my MacStill have a question?
Write to us — a real developer reads every email and replies personally.