PostgreSQL Startup Error on macOS Lion: Fixing Missing Server Configuration File


9 views

When installing PostgreSQL via Homebrew on macOS Lion, many developers encounter the frustrating error where the server fails to start due to missing configuration. The key error message reveals the problem:

postgres does not know where to find the server configuration file.
You must specify the --config-file or -D invocation option or set the PGDATA environment variable.

Homebrew installations don't automatically initialize the database cluster, which is different from typical package managers on Linux systems. You need to manually execute:

initdb /usr/local/var/postgres -E utf8

Here's the full remediation process:

# 1. Unhide the original install if you ran the hiding script
sudo mv /usr/bin/postgres /usr/bin/postgres.old.bak

# 2. Initialize the database cluster
initdb /usr/local/var/postgres -E utf8

# 3. Create the launchd plist (for auto-start)
mkdir -p ~/Library/LaunchAgents
ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents

# 4. Start the service
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

Check if PostgreSQL is running properly:

ps aux | grep postgres
pg_ctl status -D /usr/local/var/postgres

If you still encounter permission problems, try:

sudo chmod 700 /usr/local/var/postgres
sudo chown -R $(whoami) /usr/local/var/postgres

For connection issues, verify your pg_hba.conf:

local   all             all                                     trust
host    all             all             127.0.0.1/32            trust

Create a setup script to handle future installations:

#!/bin/bash
# postgres_brew_setup.sh
brew install postgresql
initdb /usr/local/var/postgres -E utf8
brew services start postgresql
createdb $(whoami)

When attempting to run PostgreSQL after a fresh Homebrew installation on macOS Lion, you might encounter this frustrating error:

postgres does not know where to find the server configuration file.
You must specify the --config-file or -D invocation option or set the PGDATA environment variable.

This occurs because PostgreSQL can't locate its data directory and configuration files. Let's break down the complete solution.

First, verify your Homebrew PostgreSQL installation:

brew info postgresql

You should see output similar to:

postgresql: stable 9.1.3 (bottled)
https://www.postgresql.org/
/usr/local/Cellar/postgresql/9.1.3 (xxxx files, xxMB)

PostgreSQL requires an initialized data directory. Run:

initdb /usr/local/var/postgres -E utf8

This creates the necessary configuration files in the default Homebrew location. If you get permission errors, try:

sudo mkdir -p /usr/local/var/postgres
sudo chown $(whoami) /usr/local/var/postgres
initdb /usr/local/var/postgres -E utf8

For automatic startup, use Homebrew's services:

brew services start postgresql

Alternatively, to run manually:

pg_ctl -D /usr/local/var/postgres start

Add this to your shell configuration (~/.bash_profile or ~/.zshrc):

export PGDATA=/usr/local/var/postgres
export PATH=/usr/local/Cellar/postgresql/9.1.3/bin:$PATH

Then reload with:

source ~/.bash_profile  # or source ~/.zshrc

If you still encounter socket errors:

rm /usr/local/var/postgres/postmaster.pid
pg_ctl -D /usr/local/var/postgres restart

For permission issues on the socket:

sudo mkdir /var/pgsql_socket
sudo ln -s /private/tmp/.s.PGSQL.5432 /var/pgsql_socket/

Check if PostgreSQL is running:

psql -l

You should see the list of available databases. If not, verify the process:

pg_ctl -D /usr/local/var/postgres status

For custom configurations, create a postgresql.conf file:

cat > /usr/local/var/postgres/postgresql.conf << EOF
listen_addresses = 'localhost'
port = 5432
max_connections = 100
EOF

Then start with explicit configuration:

postgres -D /usr/local/var/postgres