Files
PaperClipAI/packages/db
Can H. Tartanoglu 153b48dea9 fix(db): honor PostgreSQL URI host parameters for Unix sockets (#14442)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Paperclip stores control-plane state in PostgreSQL through
`packages/db`
> - A self-hosted instance can reach a local PostgreSQL service through
a Unix socket
> - libpq URIs select the socket with the `host` query parameter, but
postgres.js sends that parameter as a server setting and uses TCP
> - This pull request maps the URI `host` parameter to the driver host
option in one shared helper
> - The benefit is that app connections, migrations, backups, and
restores use the socket that the URI selects

## Linked Issues or Issue Description

**What happened?**
With
`DATABASE_URL=postgresql://paperclip@localhost:5432/paperclip?host=/run/postgresql`,
Paperclip connected over TCP to `localhost` instead of the socket.

**Expected behavior**
All JavaScript connections use the socket directory in `host`.
Percent-encoded paths also work. TCP URIs do not change.

**Steps to reproduce**
1. Run PostgreSQL with a Unix socket directory.
2. Set `DATABASE_URL` to a URI with `?host=<socket-directory>`.
3. Start Paperclip or run a JavaScript backup.
4. See that the connection uses TCP, not the socket.

**Paperclip version or commit**
upstream master cde944ed7

**Operating system**
Linux, Node 24

## What Changed

- Add `connectPostgres` in `packages/db/src/postgres-connection.ts`. It
moves the URI `host` parameter into the driver `host` option. Explicit
options still win.
- Use it in `createDb`, the utility clients, and `backup-lib.ts`.
- Key the teardown registry by the `host` parameter when it is set.
- Add tests, including a socket backup and restore with an unreachable
TCP hostname.
- Document socket URIs in `doc/DATABASE.md`.

## Verification

Rebased onto upstream `322fa6b65c` (squashed into one commit; conflicts
in `client.ts` and `client-teardown-registry.test.ts` resolved,
`codex-credentials.test.ts` taken from upstream because it already
carries the quorum fixture fix).

- `pnpm exec vitest run packages/db/src/postgres-connection.test.ts
packages/db/src/postgres-uri-host.test.ts
packages/db/src/client-teardown-registry.test.ts
packages/db/src/client.test.ts packages/db/src/backup-lib.test.ts`: 5
files, 47 tests passed.
- `pnpm --filter @paperclipai/db typecheck`: pass.
- The new tests fail when the helper ignores `host`.
- Added code comments removed per CONTRIBUTING.md; the same five test
files and `tsc` re-run after removal: pass.

## Risks

- Low risk. URIs without a `host` query parameter use the old code path.
- A URI with both a hostname and `host` now connects to `host`. This is
the libpq rule.

## Model Used

Original change: OpenAI gpt-6.1-sol via OpenCode. Cleanup and rebase:
Anthropic Claude Opus 5.5 (claude-opus-5-5), 1M context, tool use.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge
2026-12-04 20:08:24 -08:00
..
2026-03-12 13:09:22 -05:00