🐘 Fixing PostgreSQL Startup Issues on macOS (Homebrew): A Real-World Troubleshooting Guide

Introduction

Recently, I encountered an interesting PostgreSQL issue on my MacBook.

PostgreSQL was installed via Homebrew and worked perfectly on one macOS user account. However, when switching to another account on the same machine, I was unable to connect to PostgreSQL using psql.

The error looked like this:

psql postgres
psql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed:
No such file or directory
Is the server running locally and accepting connections on that socket?

This article walks through the investigation, root cause analysis, and final solution.


Understanding the Error

When PostgreSQL starts successfully, it creates a Unix socket file:

/tmp/.s.PGSQL.5432

The psql client uses this socket by default to connect to the local PostgreSQL server.

The error indicates one of two possibilities:

  1. PostgreSQL is not running.
  2. PostgreSQL is running but not listening on the expected socket.

In my case, PostgreSQL was simply not running for the current macOS user account.


Initial Verification

Verify PostgreSQL Client Installation

which psql

Output:

/opt/homebrew/bin/psql

Check version:

psql --version

Output:

psql (PostgreSQL) 14.17 (Homebrew)

This confirmed that PostgreSQL client tools were correctly installed.

Verify Installed PostgreSQL Version

brew list | grep postgres

Output:

postgresql@14

Check Whether PostgreSQL Is Running

pg_isready

Output:

/tmp:5432 - no response

This confirmed that PostgreSQL was not accepting connections.

Manual Startup Worked

Interestingly, PostgreSQL could be started manually:

/opt/homebrew/opt/postgresql@14/bin/pg_ctl \
-D /opt/homebrew/var/postgresql@14 \
-l /opt/homebrew/var/log/postgresql.log start

Output:

waiting for server to start.... done
server started

This was a critical clue.

It told us:

  • PostgreSQL binaries were healthy.
  • Database files were healthy.
  • Data directory was healthy.
  • The issue was likely related to Homebrew services or macOS LaunchAgents.

Investigating Homebrew Services

Checking service status:

brew services list

Output:

Name Status User
postgresql@14 error 78 abhilash

Attempting to start the service:

brew services start postgresql@14

Result:

Bootstrap failed: 5: Input/output error
launchctl bootstrap gui/501

This indicated a problem with the macOS LaunchAgent used by Homebrew.


Root Cause

Homebrew services rely on macOS launchctl.

Each macOS user account gets its own LaunchAgents configuration.

Although PostgreSQL was installed globally under Homebrew, the LaunchAgent configuration for this specific user account had become corrupted or stale.

As a result:

  • Manual startup worked.
  • Automatic startup through Homebrew failed.

Fixing the LaunchAgent

Stop Existing Service

brew services stop postgresql@14

Remove Existing LaunchAgent

rm ~/Library/LaunchAgents/homebrew.mxcl.postgresql@14.plist

Clean Up Homebrew Services

brew services cleanup

Verify Ownership

ls -ld /opt/homebrew/var/postgresql@14

If ownership is incorrect:

sudo chown -R $(whoami):staff /opt/homebrew/var/postgresql@14

Recreate the Service

After cleanup:

brew services start postgresql@14

Output:

Successfully started `postgresql@14`

Checking status:

brew services list

Output:

postgresql@14 started

Success!


Verifying PostgreSQL Is Running

pg_isready

Output:

/tmp:5432 - accepting connections

Connecting:

psql postgres

Output:

postgres=#

PostgreSQL was now functioning normally.


Understanding a New Error

While reviewing PostgreSQL logs, I noticed:

FATAL: database "abhilash" does not exist

At first glance, this looked concerning.

However, this is normal behavior.

When you run:

psql

PostgreSQL automatically tries to connect to a database matching your operating system username.

For example:

macOS username = abhilash

PostgreSQL attempts:

CONNECT TO abhilash;

Since that database didn’t exist, PostgreSQL logged:

FATAL: database "abhilash" does not exist

Creating a Personal Database

To make plain psql work:

CREATE DATABASE abhilash;

Now simply running:

psql

works because PostgreSQL can find a matching database.


Key Lessons Learned

1. Verify Whether PostgreSQL Is Actually Running

pg_isready

is often the fastest diagnostic tool.

2. Manual Startup Helps Isolate the Problem

If pg_ctl start works, your PostgreSQL installation and data files are probably fine.

3. Homebrew Services Depend on macOS LaunchAgents

A corrupted LaunchAgent can prevent PostgreSQL from auto-starting even when PostgreSQL itself is healthy.

4. Don’t Reinstall Immediately

Many developers jump directly to:

brew uninstall postgresql
brew install postgresql

In this case, reinstalling would not have fixed the issue and could have introduced additional problems.

5. Read the PostgreSQL Logs

Logs quickly reveal whether you’re dealing with:

  • Permission issues
  • Missing databases
  • Port conflicts
  • Startup failures
  • Authentication errors

Final Verification Checklist

brew services list
pg_isready
psql postgres

Expected results:

postgresql@14 started
/tmp:5432 - accepting connections
postgres=#

At this point, PostgreSQL is healthy and configured to start automatically after reboot.


Conclusion

What initially appeared to be a PostgreSQL installation problem turned out to be a macOS LaunchAgent issue specific to one user account.

By methodically checking:

  • PostgreSQL installation
  • Server status
  • Homebrew services
  • LaunchAgent configuration
  • PostgreSQL logs

we were able to restore automatic startup without reinstalling PostgreSQL or risking data loss.

This experience serves as a reminder that startup problems are often service-management issues rather than database issues.

Happy Debugging! 🚀

Working with docker on Mac: Core Docker Concepts | Docker Desktop

Docker is an open‑source platform for packaging applications and their dependencies into lightweight, portable units called containers, enabling consistent behavior across development, testing, and production environments (Docker, Wikipedia). Its core component, the Docker Engine, powers container execution, while Docker Desktop—a user‑friendly application tailored for macOS—bundles the Docker Engine, Docker CLI, Docker Compose, Kubernetes integration, and a visual Dashboard into a seamless package (Docker Documentation).

On macOS, Docker Desktop simplifies container workflows by leveraging native virtualization (HyperKit on Intel Macs, Apple’s Hypervisor.framework on Apple Silicon), eliminating the need for cumbersome VMs like VirtualBox (The Mac Observer, Medium).

Installation is straightforward: simply download the appropriate .dmg installer (Intel or Apple Silicon), drag Docker into the Applications folder, and proceed through setup—granting permissions and accepting licensing as needed (LogicCore Digital Blog). Once up and running, you can verify your setup via commands like:

docker --version
docker run hello-world
docker compose version

These commands confirm successful installation and provide instant access to Docker’s ecosystem on your Mac.


Commands Executed in Local System:

➜ docker --version
zsh: command not found: docker

➜ docker ps
# check the exact container name:
➜ docker ps --format "table {{.Names}}\t{{.Image}}"

# rebuild the containers
➜ docker-compose down
➜ docker-compose up --build

# Error: target webpacker: failed to solve: error getting credentials - err: exec: "docker-credential-desktop": executable file not found in $PATH, out: ``

➜ cat ~/.docker/config.json # remove "credsStore": "desktop"

# Remove all containers and images
➜ docker container prune -f
➜ docker image prune -f
➜ docker ps -a # check containers
➜ docker images # check images
➜ docker-compose up --build -d # build again

# postgres
➜ docker exec -it image-name psql -U username -d database_name

# rails console
➜ docker exec -it image-name rails c

# get into the docker container shell
➜ docker exec -it image-name bash

🐳 1. Difference between docker compose up --build and docker compose up

docker compose up

  • Just starts the containers using the existing images.
  • If the image for a service doesn’t exist locally, Docker will pull it from the registry (e.g., Docker Hub).
  • It will not rebuild your image unless you explicitly tell it to.

docker compose up --build

  • Forces Docker to rebuild the images from the Dockerfile before starting the containers.
  • Useful when you’ve changed:
    • The Dockerfile
    • Files copied into the image
    • Dependencies
  • This ensures your running containers reflect your latest code and build instructions.

📌 Example:

docker compose up         # Use existing images (fast startup)
docker compose up --build # Rebuild images before starting

If you changed your app code and your Docker setup uses bind mounts (volumes), you usually don’t need --build unless the image itself changed.
If you changed the Dockerfile, then you need --build.


🖥 2. Why we use Docker Desktop & can we use Docker without it?

Docker Desktop is basically a GUI + background service that makes Docker easy to run on macOS and Windows.
It includes:

  • Docker Engine (runs containers)
  • Docker CLI
  • Docker Compose
  • Kubernetes (optional)
  • Settings & resource controls (CPU, RAM)
  • Networking setup
  • A UI to view containers, images, logs, etc.

Why needed on macOS & Windows?

  • Docker requires Linux kernel features like cgroups & namespaces.
  • macOS and Windows don’t have these natively, so Docker Desktop runs a lightweight Linux VM behind the scenes (using HyperKit, WSL2, etc.).
  • Without Docker Desktop, you’d need to set up that Linux VM manually, install Docker inside it, and configure networking — which is more complex.

Can you use Docker without Docker Desktop?
Yes, but:

  • On macOS/Windows — you’d have to:
    • Install a Linux VM manually (VirtualBox, VMware, UTM, etc.)
    • SSH into it
    • Install Docker Engine
    • Expose ports and share files manually
  • On Linux — you don’t need Docker Desktop at all, you can install Docker Engine directly via: sudo apt install docker.io
  • For Windows, Microsoft has Docker on WSL2 which can run without the Docker Desktop GUI, but requires WSL2 setup.

💡 In short:

  • Use --build when you change something in the image definition.
  • Docker Desktop = easiest way to run Docker on macOS/Windows.
  • You can skip Docker Desktop, but then you must manually set up a Linux VM with Docker.

🧩 1. Core Docker Concepts

TermWhat it isKey analogy
ImageA read-only blueprint (template) that defines what your app needs to run (OS base, packages, configs, your code). Built from a Dockerfile.Like a recipe for a dish
ContainerA running instance of an image. Containers are isolated processes, not full OSes.Like a meal prepared from the recipe
VolumePersistent storage for containers. Survives container restarts or deletions.Like a pantry/fridge where food stays even after cooking is done
Docker ComposeA YAML-based tool to define & run multi-container apps. Lets you describe services, networks, and volumes in one file and start them all at once.Like a restaurant order sheet for multiple dishes at once
NetworkVirtual network that containers use to talk to each other or the outside world.Like a kitchen intercom system

2. Kubernetes in simple words

Kubernetes (K8s) is a container orchestration system. It’s what you use when you have many containers across many machines and you need to manage them automatically.

What it does:

  • Deploy containers on a cluster of machines
  • Restart them if they crash
  • Scale up/down automatically
  • Load balance traffic between them
  • Handle configuration and secrets
  • Do rolling updates with zero downtime

📌 Analogy
If Docker Compose is like cooking multiple dishes at home, Kubernetes is like running a huge automated kitchen in a restaurant chain — you don’t manually turn on each stove; the system manages resources and staff.


🍏 3. How Docker Works on macOS

Your assumption is right — Docker needs Linux kernel features (cgroups, namespaces, etc.), and macOS doesn’t have them.

So on macOS:

  • Docker Desktop runs a lightweight Linux virtual machine under the hood using Apple’s HyperKit (before) or Apple Virtualization Framework (newer versions).
  • That VM runs the Docker Engine.
  • Your docker CLI in macOS talks to that VM over a socket.
  • All containers run inside that Linux VM, not directly on macOS.

Workflow:

Mac Terminal → Docker CLI → Linux VM in background → Container runs inside VM


4. Hardware Needs for Docker on macOS

Yes, VMs can be heavy, but Docker’s VM for macOS is minimal — not like a full Windows or Ubuntu desktop VM.

Typical Docker Desktop VM:

  • Base OS: Tiny Linux distro (Alpine or LinuxKit)
  • Memory: Usually 2–4 GB (configurable)
  • CPU: 2–4 virtual cores (configurable)
  • Disk: ~1–2 GB base, plus images & volumes you pull

Recommended host machine for smooth Docker use on macOS:

  • RAM: At least 8 GB (16 GB is comfy)
  • CPU: Modern dual-core or quad-core
  • Disk: SSD (fast read/write for images & volumes)

💡 Reason it’s lighter than “normal” VMs:
Docker doesn’t need a full OS with GUI in its VM — just the kernel & minimal services to run containers.


Quick Recap Table:

TermPurposePersistent?
ImageApp blueprintYes (stored on disk)
ContainerRunning app from imageNo (dies when stopped unless data in volume)
VolumeData storage for containersYes
ComposeMulti-container managementYes (config file)
KubernetesCluster-level orchestrationN/A

Quickest way to see per-request Rails logs in Docker

  • Run app logs:
docker compose logs -f --tail=200 main-app
  • Run Sidekiq logs:
docker compose logs -f --tail=200 sidekiq
  • Filter for a single request by its request ID (see below):
docker compose logs -f main-app | rg 'request_id=YOUR_ID'

Ensure logs are emitted to STDOUT (so Docker can collect them)

Your images already set RAILS_LOG_TO_STDOUT=true and the app routes logs to STDOUT:

if ENV["RAILS_LOG_TO_STDOUT"].present?
  logger           = ActiveSupport::Logger.new(STDOUT)
  logger.formatter = config.log_formatter
  config.log_tags  = [:subdomain, :uuid]
  config.logger    = ActiveSupport::TaggedLogging.new(logger)
end

So the docker compose logs commands above are the right way. Tailing log files inside the container usually isn’t needed, but if you want to:

docker compose exec main-app bash -lc 'tail -f log/development.log'

Trace a single request end-to-end

  • Get the request ID from the response (Rails sets X-Request-Id):
REQ=$(curl -s -D - http://localhost:3001/your/path -o /dev/null | awk '/X-Request-Id/ {print $2}' | tr -d '\r')
docker compose logs -f main-app | rg "$REQ"
  • Your app tags logs with a UUID per request; switching to the canonical tag makes grepping easier. Optionally change tags to:
# in `website-v1/config/application.rb`
config.log_tags = [:request_id]

Make logs easier to read (optional, but highly recommended)

  • Add concise, per-request logging (1 line per request) with JSON output for easy grep/parse:
# Gemfile
gem 'lograge'

# config/initializers/lograge.rb
Rails.application.configure do
  config.lograge.enabled = true
  config.lograge.formatter = Lograge::Formatters::Json.new
  config.lograge.custom_options = lambda do |event|
    {
      request_id: event.payload[:request_id],
      params: event.payload[:params].except('controller','action'),
      user_id: event.payload[:user_id],
      time: Time.now.utc
    }
  end
end

Then:

bundle install
docker compose restart main-app
docker compose logs -f main-app

Now you can grep easily:

docker compose logs -f main-app | jq -r 'select(.request_id=="'"$REQ"'")'

Tuning

  • In development, you already have config.log_level = :debug. For production, consider :info instead of :warn to get request lines:
# config/environments/production.rb
config.log_level = :info
  • Sidekiq logs are a separate stream (service sidekiq); use its logs for background job tracing.
  • Noise reduction: you already have config.assets.quiet = true in development.

If you want, I can switch to :request_id tagging and add lograge with JSON in the repo now.

  • Made no code edits; provided commands to stream logs from main-app and sidekiq.
  • Confirmed logs are routed to STDOUT via RAILS_LOG_TO_STDOUT and TaggedLogging in application.rb.
  • Offered optional improvements (switch to :request_id, add lograge JSON) and how to grep a single request by ID.