{"record":{"id":"6fbae83812da09d0","repo":"paperclipai/paperclip","slug":"cannot-seed-target-embedded-postgresql-at-datadi","errorCode":null,"errorMessage":"Cannot seed target embedded PostgreSQL at ${dataDir} while it is already running (pid=${runningPid}). Stop the worktree service that owns this database, then retry the seed.","messagePattern":"Cannot seed target embedded PostgreSQL at (.+?) while it is already running \\(pid=(.+?)\\)\\. Stop the worktree service that owns this database, then retry the seed\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"cli/src/commands/worktree.ts","lineNumber":1163,"sourceCode":"  options: { allowExisting?: boolean } = {},\n): Promise<EmbeddedPostgresHandle> {\n  const moduleName = \"embedded-postgres\";\n  let EmbeddedPostgres: EmbeddedPostgresCtor;\n  try {\n    const mod = await loadWithoutEmbeddedPostgresExitHooks(() => import(moduleName));\n    EmbeddedPostgres = mod.default as EmbeddedPostgresCtor;\n  } catch {\n    throw new Error(\n      \"Embedded PostgreSQL support requires dependency `embedded-postgres`. Reinstall dependencies and try again.\",\n    );\n  }\n  await prepareEmbeddedPostgresNativeRuntime();\n\n  const postmasterPidFile = path.resolve(dataDir, \"postmaster.pid\");\n  const runningPid = readRunningPostmasterPid(postmasterPidFile);\n  if (runningPid) {\n    if (options.allowExisting === false) {\n      throw new Error(\n        `Cannot seed target embedded PostgreSQL at ${dataDir} while it is already running (pid=${runningPid}). `\n        + \"Stop the worktree service that owns this database, then retry the seed.\",\n      );\n    }\n    return {\n      port: readPidFilePort(postmasterPidFile) ?? preferredPort,\n      startedByThisProcess: false,\n      stop: async () => {},\n    };\n  }\n\n  const port = await findAvailablePort(preferredPort);\n  const logBuffer = createEmbeddedPostgresLogBuffer();\n  const instance = new EmbeddedPostgres({\n    databaseDir: dataDir,\n    user: \"paperclip\",\n    password: \"paperclip\",\n    port,","sourceCodeStart":1145,"sourceCodeEnd":1181,"githubUrl":"https://github.com/paperclipai/paperclip/blob/a7e689b3c35347b529cb9f54c9b9a8575a3dcab6/cli/src/commands/worktree.ts#L1145-L1181","documentation":"ensureEmbeddedPostgres() (cli/src/commands/worktree.ts:1142) prepares a target worktree's embedded PostgreSQL for seeding. It reads postmaster.pid in the data dir; if a live postmaster PID is found and options.allowExisting === false, it refuses to seed because another process (typically the worktree's own running service) owns that database. Seeding needs exclusive control of the cluster so the snapshot/clone is deterministic. The PID and data dir are printed for diagnosis.","triggerScenarios":"Running worktree ensure-seeded / reseed / repair while the target worktree's service is still up and its embedded PostgreSQL is running; any seed call that passes allowExisting: false against a data dir whose postmaster.pid names a live process.","commonSituations":"Forgetting to stop the worktree service before reseeding; a second terminal still running the service for that worktree; a leftover postmaster from a crashed service keeping the PID alive.","solutions":["Stop the worktree service that owns the database (the process holding the PID named in the error), then re-run the seed","If no service is visibly running, verify the PID with `ps -p <pid>`; a live PID means some postmaster still holds the cluster","As a last resort, shut that PostgreSQL instance down cleanly (its postmaster PID responds to a normal stop) so the seed can start its own"],"exampleFix":"# before: target worktree service still running\n$ paperclipai worktree ensure-seeded\nError: Cannot seed target embedded PostgreSQL at .../pgdata while it is already running (pid=12345)\n\n# after\n$ ps -p 12345          # identify the owning service\n$ <stop that worktree service>\n$ paperclipai worktree ensure-seeded","handlingStrategy":"retry","validationCode":"import { existsSync, readFileSync } from 'node:fs';\nimport path from 'node:path';\n\nconst pidFile = path.resolve(dataDir, 'postmaster.pid');\nfunction postmasterAlive(): boolean {\n  if (!existsSync(pidFile)) return false;\n  const pid = Number.parseInt(readFileSync(pidFile, 'utf8').split('\\n')[0] ?? '', 10);\n  if (!Number.isInteger(pid)) return false;\n  try { process.kill(pid, 0); return true; } catch { return false; }\n}\nif (postmasterAlive()) await stopOwningWorktreeService(); // before seeding","typeGuard":null,"tryCatchPattern":"Catch the 'already running (pid=' message, surface the owning PID, stop that service, and re-run the seed once (bounded retry).","preventionTips":["Always stop the target worktree service before seed/reseed/repair","Never share one embedded data dir between two services","Treat a live postmaster.pid as authoritative; never delete it to force a seed"],"tags":["postgres","worktree","seed","concurrency","embedded-postgres"],"backgroundTag":"postgres-already-running","analyzedSha":"a7e689b3c35347b529cb9f54c9b9a8575a3dcab6","analyzedAt":"2026-08-21T17:58:32.592Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}