From e48a871d0cbbe804f5e8384a937c1163a823b720 Mon Sep 17 00:00:00 2001 From: zbigniew sobiecki Date: Thu, 1 Jan 2026 21:18:32 +0100 Subject: [PATCH] fix: improve PostgreSQL startup reliability and agent documentation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add robust error handling to startPostgres() that fails fast if PostgreSQL cannot start, preventing agent sessions from proceeding with a broken database - Configure PostgreSQL in Dockerfile with password authentication: - User: postgres, Password: postgres - Connection string: postgresql://postgres:postgres@localhost:5432/postgres - Local socket uses trust, TCP connections require md5 password - Update agent environment prompt with complete PostgreSQL documentation: - Connection string and CLI access - Start/stop/status commands using pg_ctl - Database creation example This addresses issues where agents would waste iterations trying to troubleshoot PostgreSQL connectivity using incorrect commands (systemctl, brew services) that don't work in the container environment. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- Dockerfile | 16 +++++- .../templates/partials/environment.eta | 8 ++- src/agents/utils/setup.ts | 51 ++++++++++++++++--- 3 files changed, 67 insertions(+), 8 deletions(-) diff --git a/Dockerfile b/Dockerfile index 37310c5f..ef8d58c5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -31,9 +31,23 @@ RUN apt-get update && apt-get install -y \ && ln -s $(which fdfind) /usr/local/bin/fd # Configure PostgreSQL for local development use by agents +# - User: postgres, Password: postgres +# - Connection: postgresql://postgres:postgres@localhost:5432/postgres RUN mkdir -p /run/postgresql && chown -R postgres:postgres /run/postgresql \ && mkdir -p /var/lib/postgresql/data && chown -R postgres:postgres /var/lib/postgresql \ - && su postgres -c "/usr/lib/postgresql/*/bin/initdb -D /var/lib/postgresql/data" + && su postgres -c "/usr/lib/postgresql/*/bin/initdb -D /var/lib/postgresql/data" \ + && { \ + echo "# PostgreSQL Client Authentication Configuration"; \ + echo "# TYPE DATABASE USER ADDRESS METHOD"; \ + echo "local all all trust"; \ + echo "host all all 127.0.0.1/32 md5"; \ + echo "host all all ::1/128 md5"; \ + echo "host all all 0.0.0.0/0 md5"; \ + } > /var/lib/postgresql/data/pg_hba.conf \ + && chown postgres:postgres /var/lib/postgresql/data/pg_hba.conf \ + && su postgres -c "/usr/lib/postgresql/*/bin/pg_ctl start -D /var/lib/postgresql/data -l /tmp/postgres.log -w" \ + && su postgres -c "psql -c \"ALTER USER postgres WITH PASSWORD 'postgres';\"" \ + && su postgres -c "/usr/lib/postgresql/*/bin/pg_ctl stop -D /var/lib/postgresql/data" # Install ast-grep RUN ARCH=$(dpkg --print-architecture) && \ diff --git a/src/agents/prompts/templates/partials/environment.eta b/src/agents/prompts/templates/partials/environment.eta index 1f954f27..20ac381b 100644 --- a/src/agents/prompts/templates/partials/environment.eta +++ b/src/agents/prompts/templates/partials/environment.eta @@ -1,6 +1,12 @@ ## Available Environment -- **PostgreSQL**: Local server running. Connect via `psql -U postgres`. +- **PostgreSQL**: Local server running on port 5432 + - **Connection string**: `postgresql://postgres:postgres@localhost:5432/postgres` + - **Connect via CLI**: `psql -U postgres -h localhost` + - **Start**: `su postgres -c 'pg_ctl start -D /var/lib/postgresql/data -l /tmp/postgres.log'` + - **Stop**: `su postgres -c 'pg_ctl stop -D /var/lib/postgresql/data'` + - **Status**: `su postgres -c 'pg_ctl status -D /var/lib/postgresql/data'` + - **Create database**: `psql -U postgres -h localhost -c 'CREATE DATABASE mydb;'` - **Node.js 22**: With npm, pnpm, yarn, bun - **Git & GitHub CLI**: `git` and `gh` commands available - **Search tools** (use via Tmux for fast codebase exploration): diff --git a/src/agents/utils/setup.ts b/src/agents/utils/setup.ts index 7d815d0d..8e041e86 100644 --- a/src/agents/utils/setup.ts +++ b/src/agents/utils/setup.ts @@ -13,14 +13,53 @@ let postgresStarted = false; export async function startPostgres(): Promise { if (postgresStarted) return; + const PG_DATA = '/var/lib/postgresql/data'; + const PG_LOG = '/tmp/postgres.log'; + + try { + // Check if PostgreSQL is already running + const statusResult = await execCommand( + 'su', + ['postgres', '-c', `pg_ctl status -D ${PG_DATA}`], + '/', + ); + + if (statusResult.stdout.includes('server is running')) { + logger.info('PostgreSQL already running'); + postgresStarted = true; + return; + } + } catch { + // Not running, continue to start it + } + // Start PostgreSQL as postgres user - await execCommand( - 'su', - ['postgres', '-c', 'pg_ctl start -D /var/lib/postgresql/data -l /tmp/postgres.log'], - '/', - ); + logger.info('Starting PostgreSQL...'); + try { + await execCommand('su', ['postgres', '-c', `pg_ctl start -D ${PG_DATA} -l ${PG_LOG} -w`], '/'); + } catch (err) { + logger.error('Failed to start PostgreSQL', { error: String(err) }); + throw new Error(`PostgreSQL failed to start. Check ${PG_LOG} for details. Error: ${err}`); + } + + // Verify it's actually running + try { + const verifyResult = await execCommand( + 'su', + ['postgres', '-c', `pg_ctl status -D ${PG_DATA}`], + '/', + ); + + if (!verifyResult.stdout.includes('server is running')) { + throw new Error('PostgreSQL started but status check failed'); + } + } catch (err) { + logger.error('PostgreSQL status check failed after start', { error: String(err) }); + throw new Error(`PostgreSQL failed to start properly: ${err}`); + } + postgresStarted = true; - logger.info('PostgreSQL started'); + logger.info('PostgreSQL started successfully'); } // ============================================================================