Skip to content

Fall back to $PGSERVICE when no connection string is set - #36

Merged
alexshapalov merged 2 commits into
pgrundev:mainfrom
bluzername:pgservice-fallback
Sep 6, 2026
Merged

Fall back to $PGSERVICE when no connection string is set#36
alexshapalov merged 2 commits into
pgrundev:mainfrom
bluzername:pgservice-fallback

Conversation

@bluzername

Copy link
Copy Markdown
Contributor

Closes #25.

pgx's ParseConfig already knows how to read a connection service
file
- it
work fine if you give it service=myname. But pgbot return "no
connection string" and stop, before pgx even get a chance, when the only
thing set is $PGSERVICE.

I test this local: with PGSERVICEFILE and PGSERVICE set and nothing
else, pgxpool.ParseConfig("service=myname") resolve the host/port/user
from the service file correctly. So the fix is small - just add
$PGSERVICE as one more fallback in the same firstNonEmpty(...) chain
every command already use, after $DATABASE_URL / $PGBOT_DATABASE_URL,
same order the issue want.

What change:

  • pgServiceFallback() in helpers.go turn a bare $PGSERVICE into
    "service=<name>", which pgx already understand.
  • All 18 call sites (inspect, queries, vacuum, mcp, and the rest)
    now pass it as the last fallback.
  • Test for the new helper, plus a fix to TestDsnFromArgs so it also
    clear $PGSERVICE in its "no connection string" case (else that test
    become flaky on a machine that has a service file configured).
  • README: mention $PGSERVICE next to $DATABASE_URL in the 3 places
    that already document the fallback order.

An explicit argument or $DATABASE_URL/$PGBOT_DATABASE_URL still win,
nothing change there. go test ./... pass, gofmt/go vet clean.

bluzername and others added 2 commits September 6, 2026 07:22
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
@alexshapalov

Copy link
Copy Markdown
Contributor

Code review

No 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: service=<name> is resolved through jackc/pgservicefile, honoring PGSERVICEFILE, including sslmode from the file. The PR's own test only covered building the string, so I pushed c15be83 to this branch (maintainer edits) with a test that writes a temp service file and asserts pgx applies its host, port, user, database, and sslmode, and that an unknown service name errors rather than falling through to localhost.

// pgServiceFallback lets a bare $PGSERVICE select a connection when neither an
// argument nor $DATABASE_URL/$PGBOT_DATABASE_URL is set. pgx's ParseConfig
// already reads a connection service file (PGSERVICEFILE, or the libpq
// default path) once it gets a "service=..." string — this just builds that
// string so users who manage connections through a service file don't have
// to also pass one explicitly.
func pgServiceFallback() string {
if svc := os.Getenv("PGSERVICE"); svc != "" {
return "service=" + svc
}
return ""
}

One nit left alone: the "no connection string (pass one or set $DATABASE_URL)" message in each command doesn't mention $PGSERVICE; not worth touching 18 files for.

🤖 Generated with Claude Code

- If this code review was useful, please react with 👍. Otherwise, react with 👎.

@alexshapalov
alexshapalov merged commit 44c253f into pgrundev:main Sep 6, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support connection service file

2 participants