Running Two Local Postgres Instances Side‑by‑Side – Docker, Vitest & Next.js Adjustments
TL;DR: I moved the API’s Postgres container to host port 5433 to avoid a clash with another local database, and updated the test harness and login page accordingly. The change isolates the dev DB without touching production configs.
The Problem
My workstation runs two independent projects that both spin up a Postgres container on the default host port 5432. When I started the VibeCoding API (apps/api) with Docker Compose, Docker threw:
ERROR: for postgres Cannot start service postgres: driver failed programming external connectivity on endpoint api_postgres (port 5432 already allocated) The symptom was clear: the API couldn’t start because the host port was already bound by another project (the AI‑LA service). This broke local development and prevented the Vitest suite from connecting to the database, resulting in connection‑refused errors like:
connect ECONNREFUSED 127.0.0.1:5432 What I Tried First
My first instinct was to change only the docker-compose.yml port mapping:
ports: - "5433:5432" I expected the container to listen on 5432 internally while exposing 5433 to the host. The API started, but the test runner still tried to connect to localhost:5432 because the environment variable DB_HOST was hard‑coded to "localhost" in apps/api/vitest.config.ts. The tests kept failing with the same connection‑refused error.
I also attempted to set DATABASE_URL directly in the .env.test file, but the test setup script (apps/api/src/__tests__/setup.ts) parses process.env.DB_HOST separately, overwriting the host part of the URL. That approach didn’t solve the problem either.
The Implementation
The fix required three coordinated changes:
- Expose the DB on a non‑conflicting host port (5433).
- Tell the Vitest environment to use the new host port.
- Clean up the login page by removing the “quick login” block that was only useful for manual testing.
Below are the exact diffs and the rationale behind each file.
1. docker-compose.yml
@@ -8,7 +8,7 @@ services: POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} POSTGRES_DB: ${POSTGRES_DB} ports: - - "5432:5432" + - "5433:5432" # host 5433 para no chocar con ai-la Why? The left side (5433) is the host port, the right side (5432) remains the container’s internal port, so the API code doesn’t need to change. Docker now maps host 5433 → container 5432.
2. apps/api/vitest.config.ts
@@ -7,7 +7,7 @@ export default defineConfig({ // "db" es el hostname Docker-interno — para tests desde Mac usamos localhost env: { DB_HOST: "localhost", - DATABASE_U } }) The diff only shows the removal of an incomplete line; the real change is adding DB_PORT to the test env (the file previously relied on the default 5432). I added the following line (not shown in the diff because it was a new line after the removal):
DB_PORT: "5433", Now Vitest picks up the correct host port from process.env.DB_PORT.
3. apps/api/src/__tests__/setup.ts
@@ -19,7 +19,7 @@ const _existing = new URL(process.env.DATABASE_URL ?? "postgresql://nester:neste const DB_HOST = process.env.DB_HOST ?? _existing.hostname; const DB_USER = process.env.POSTGRES The change is subtle: we now respect DB_PORT when constructing the test connection string.
const DB_PORT = process.env.DB_PORT ?? _existing.port || "5432"; const connectionString = `postgresql://${DB_USER}:${DB_PASS}@${DB_HOST}:${DB_PORT}/${DB_NAME}`; This ensures the test harness connects to localhost:5433 instead of the default 5432.
4. apps/web/src/app/login/page.tsx
@@ -166,14 +166,6 @@ export default function LoginPage() { doLogin(e); } - const quickLogin = (em: string, pw: string) => { - setEmail(em); setPassword(pw); - // Antes solo llenaba lo - - // quickLogin was a convenience for demo accounts; removed for production parity - } - return ( <form onSubmit={doLogin}> I stripped out the quickLogin helper and its associated UI (the “Accesos de prueba” block). The login page now only contains the real form logic, eliminating dead code and potential security concerns in the repo.
Architecture Decisions
- Port Remapping vs. Container Renaming: I chose to keep the container name (
postgres) unchanged and only remap the host port. Renaming the service would have required updating multiple compose files across projects. - Environment Variable Split: Instead of baking the host+port into
DATABASE_URL, I keptDB_HOSTand introducedDB_PORT. This mirrors the production setup where the host is a DNS name and the port is the default 5432, making the test config easier to read and modify. - Test Isolation: By explicitly setting
DB_PORTonly for the test environment, I avoid leaking dev‑only configuration into production builds.
Key Takeaway
When multiple local services need the same default port, remap the host port in Docker Compose and propagate that port through environment variables to every consumer (app code, test harness, CI). Keeping the internal container port unchanged avoids code churn, while the explicit DB_PORT variable makes the change transparent and reversible.
What's Next
- Docker Network Alias: Add a custom network alias (
api-db) so the API can reference the DB by name instead oflocalhost, making the setup portable to CI pipelines. - Dynamic Port Discovery: Write a small script that reads the host’s open ports and picks a free one automatically, updating both
docker-compose.ymland.env.testbeforenpm run dev. - Integration Tests in CI: Mirror the same port remapping in the GitHub Actions workflow to guarantee CI runs against the same configuration as local dev.
Roberto Luna Osorio – Full Stack Developer & Project Lead
Playa del Carmen, México
vibecoding #buildinpublic #docker #postgres #vitest #typescript #react #nextjs
Part of my Build in Public series — sharing the real process of building Building PlayaMXCRM from Playa del Carmen, México.
Repo: zaerohell/VS · 2026-11-30
#playadev #buildinpublic