Fall back to $PGSERVICE when no connection string is set - #36
Conversation
Closes pgrundev#25. pgx ParseConfig already read a connection service file fine if you give it "service=myname", but pgbot say "no connection string" before it even try, when you only set PGSERVICE and nothing else. Now it check PGSERVICE too, same order as DATABASE_URL, so people who manage many databases with a service file do not need to also export a full connection string just for pgbot. An explicit argument or DATABASE_URL still win over PGSERVICE, same as before.
…ERVICEFILE The helper test proves the "service=<name>" string is built; this proves the part the feature actually rests on — pgx reading host, port, user, database, and sslmode for that name out of the connection service file — and that an unknown service name is an error rather than a silent fall-through. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013qGZKWgfGTBCoHsDjy1SuB
Code reviewNo issues found. Checked for bugs, all 18 call sites (none left without the fallback), the pgx side of the claim, and docs; this repo has no CLAUDE.md. Verified that pgx v5.10 does what the description relies on: Lines 27 to 38 in cfc2789 One nit left alone: the "no connection string (pass one or set $DATABASE_URL)" message in each command doesn't mention 🤖 Generated with Claude Code - If this code review was useful, please react with 👍. Otherwise, react with 👎. |
Closes #25.
pgx's
ParseConfigalready knows how to read a connection servicefile - it
work fine if you give it
service=myname. But pgbot return "noconnection string" and stop, before pgx even get a chance, when the only
thing set is
$PGSERVICE.I test this local: with
PGSERVICEFILEandPGSERVICEset and nothingelse,
pgxpool.ParseConfig("service=myname")resolve the host/port/userfrom the service file correctly. So the fix is small - just add
$PGSERVICEas one more fallback in the samefirstNonEmpty(...)chainevery command already use, after
$DATABASE_URL/$PGBOT_DATABASE_URL,same order the issue want.
What change:
pgServiceFallback()inhelpers.goturn a bare$PGSERVICEinto"service=<name>", which pgx already understand.inspect,queries,vacuum,mcp, and the rest)now pass it as the last fallback.
TestDsnFromArgsso it alsoclear
$PGSERVICEin its "no connection string" case (else that testbecome flaky on a machine that has a service file configured).
$PGSERVICEnext to$DATABASE_URLin the 3 placesthat already document the fallback order.
An explicit argument or
$DATABASE_URL/$PGBOT_DATABASE_URLstill win,nothing change there.
go test ./...pass,gofmt/go vetclean.