fix(postgres): connect over a Unix socket

TREK_DB_URL could not name a Unix socket. MikroORM builds the pool from the
URL's own host and drops a host parameter, so
postgresql://trek@localhost/trek?host=/var/run/postgresql connected to
localhost over TCP. A percent-encoded socket path as the host reached the
pool, but the client tools got it undecoded in PGHOST, so backups, restores
and the safety copy before a migration failed.

The server address is now read once, the way libpq reads a connection URL:
a host or port parameter takes the place of the URL's own, and a host
written into the URL is percent-decoded. The pool and the client tools both
get that address. The wiki and .env.example show the socket form.
This commit is contained in:
Maurice
2026-11-24 23:15:26 +01:00
parent aa79d6286a
commit f928f4b1ca
8 changed files with 100 additions and 4 deletions
+5 -2
View File
@@ -1,3 +1,5 @@
import { pgServerAddress } from './server-address';
import { execFile, spawn } from 'node:child_process';
import fs from 'node:fs';
import path from 'node:path';
@@ -65,9 +67,10 @@ export function resolvePgTool(tool: PgTool, binDir: string | undefined): string
*/
export function pgConnectionEnv(url: string): Record<string, string> {
const parsed = new URL(url);
const address = pgServerAddress(url);
const env: Record<string, string> = {
PGHOST: parsed.hostname.replace(/^\[(.*)\]$/, '$1'),
PGPORT: parsed.port || '5432',
PGHOST: address.host,
PGPORT: String(address.port ?? 5432),
PGDATABASE: decodeURIComponent(parsed.pathname.replace(/^\//, '')),
PGCONNECT_TIMEOUT: CONNECT_TIMEOUT_SECONDS,
};