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,6 +5,7 @@ import type { DatabaseEngine } from '../engine';
import { redactUrl } from '../redact-url';
import { TrekPostgreSqlDriver } from './postgres-driver';
import { bootstrapPostgresSchema, defaultPostgresBootstrapDeps, type PostgresBootstrapDeps } from './schema-bootstrap';
import { pgServerAddress } from './server-address';
import { putTempSchemaLast } from './session-search-path';
import { sslOptionFor } from './ssl';
import type { EntityManager } from '@mikro-orm/core';
@@ -84,9 +85,13 @@ export class PostgresEngine implements DatabaseEngine {
/** Every new pool connection first puts `pg_temp` last on its `search_path` (`session-search-path.ts`). */
ormOptions() {
const ssl = sslOptionFor(this.settings.url);
// MikroORM takes the address from the URL's authority alone; a `host` parameter (a socket directory) would be lost.
const { host, port } = pgServerAddress(this.settings.url);
return {
driver: TrekPostgreSqlDriver,
clientUrl: this.settings.url,
host,
port,
pool: { min: 0, max: this.settings.poolMax },
driverOptions: ssl === undefined ? {} : { ssl },
onCreateConnection: (connection: unknown) => putTempSchemaLast(connection as DatabaseConnection),