Fix brew postgres service error

Published on
Fix brew postgres service error

Recently, I installed PostgreSQL 16 via Homebrew on my macOS system using:

brew install postgresql@16
brew services start postgresql@16

It seemed like everything went smoothly:

Successfully started `postgresql@16` (label: homebrew.mxcl.postgresql@16)

But when I checked the service status using brew services, I saw:

postgresql@16 error  1 markoburazin ~/Library/LaunchAgents/homebrew.mxcl.postgresql@16.plist

Hmm. Not so smooth after all. Here’s how I diagnosed and resolved it.

🔍 Step 1: Check why postgres service fails to start

Run PostgreSQL manually to see the real issue:

/opt/homebrew/opt/postgresql@16/bin/postgres -D /opt/homebrew/var/postgresql@16

Example of output:

2025-04-16 17:01:00.412 CEST [56476] LOG:  starting PostgreSQL 16.8 (Homebrew) on aarch64-apple-darwin24.2.0, compiled by Apple clang version 16.0.0 (clang-1600.0.26.6), 64-bit
2025-04-16 17:01:00.414 CEST [56476] LOG:  could not bind IPv4 address "127.0.0.1": Address already in use
2025-04-16 17:01:00.414 CEST [56476] HINT:  Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
2025-04-16 17:01:00.414 CEST [56476] LOG:  could not bind IPv6 address "::1": Address already in use
2025-04-16 17:01:00.414 CEST [56476] HINT:  Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
2025-04-16 17:01:00.414 CEST [56476] WARNING:  could not create listen socket for "localhost"
2025-04-16 17:01:00.414 CEST [56476] FATAL:  could not create any TCP/IP sockets
2025-04-16 17:01:00.415 CEST [56476] LOG:  database system is shut down

In this case the clue is right there — PostgreSQL can’t bind to port 5432 because something else is already using it.

🔍 Step 2: Check what’s using port 5432

Run the following:

lsof -i :5432

I got:

COMMAND   PID   USER     FD   TYPE             DEVICE SIZE/OFF NODE NAME
postgres  904   markoburazin   6u  IPv4 ...   TCP localhost:postgresql (LISTEN)

Would you look at that — in this case another PostgreSQL instance (an old version or a lingering process) was already using that port.

✅ Step 3: Kill the offending process

Quick and effective:

kill <PID>

In my case PID was 904. Now, I was able to restart PostgreSQL 16 successfully:

brew services start postgresql@16

And the service started without error. 🎉

🧰 Bonus: How to prevent this in the future

Check for any existing versions (especially if you want to update to new Postgres version):

brew services list

Then stop and uninstall older versions you don’t need:

brew services stop postgresql@12
brew uninstall postgresql@12

And only then install the new PostgreSQL version:

brew install postgresql@14

Running multiple PostgreSQL versions

Alternatively, if you do want to run multiple versions side-by-side, just change the port in postgresql.conf:

vi /opt/homebrew/var/postgresql@16/postgresql.conf

Change the default port number:

port = 5432

To some other port, like:

port = 5433

Then restart the service using brew:

brew services restart postgresql@16