Skip to content

Look up service files like libpq, fixing a crash on Windows - #1658

Open
b84330808 wants to merge 2 commits into
dbcli:mainfrom
b84330808:fix-windows-service-file
Open

b84330808 wants to merge 2 commits into
dbcli:mainfrom
b84330808:fix-windows-service-file

Conversation

@b84330808

@b84330808 b84330808 commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Description

On Windows, pgcli service=mydb (or PGSERVICE=mydb pgcli) crashes unless PGSERVICEFILE or PGSYSCONFDIR is set:

  File "...\pgcli\main.py", line 2282, in parse_service_info
    service_file = os.getenv("PGSYSCONFDIR") + "\pg_service.conf"
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

PGSYSCONFDIR is usually unset, so the Windows branch adds None to a string.

parse_service_info now looks files up the way libpq's parseServiceInfo does:

  • PGSERVICEFILE if set, otherwise the per-user file: %APPDATA%\postgresql\.pg_service.conf on Windows (docs), ~/.pg_service.conf elsewhere.
  • If that file is missing or doesn't have the service, $PGSYSCONFDIR/pg_service.conf, with no leading dot.
  • With APPDATA unset on Windows there is no per-user file, rather than a path relative to the current directory.

So $PGSYSCONFDIR no longer wins over the per-user file, and $PGSYSCONFDIR/.pg_service.conf is no longer read. libpq also falls back to a compiled-in SYSCONFDIR when PGSYSCONFDIR is unset, and pgcli can't know that path, so that case isn't covered.

The new tests fake platform.system() so they run on the Linux CI too. Run on Windows with pytest tests/test_main.py -k service: 13 pass, and 5 fail against main.

Checklist

  • I've added this contribution to the changelog.rst.
  • I've added my name to the AUTHORS file (or it's already there).
  • I installed pre-commit hooks (pip install pre-commit && pre-commit install).
  • I verified that my changes work as expected (this may include manually testing them in your local environment, or in other available environments). Cross this out if not relevant (for example, if you're making a documentation change).
  • Please squash merge this pull request (uncheck if you'd like us to merge as multiple commits)

On Windows, parse_service_info() built the service file path as
os.getenv("PGSYSCONFDIR") + "\pg_service.conf", so `pgcli service=foo`
or PGSERVICE crashed with a TypeError whenever PGSYSCONFDIR was unset,
which is the default.

Fall back to libpq's per-user service file on Windows,
%APPDATA%\postgresql\.pg_service.conf, when PGSYSCONFDIR is not set.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@DiegoDAF

DiegoDAF commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

Thanks, this fixes the crash.

Two things in the same function, in case you want to fold them in (happy to do a follow-up otherwise): libpq reads the per-user file first and only falls back to $PGSYSCONFDIR/pg_service.conf when the service isn't there, and the system file has no leading dot (pg_service.conf, see parseServiceInfo in fe-connect.c).

Also, with APPDATA unset the path becomes relative to the current directory.

Read the per-user file first and fall back to
$PGSYSCONFDIR/pg_service.conf when the service isn't there. The system
file has no leading dot. With APPDATA unset on Windows there is no
per-user file, instead of a path relative to the current directory.
@b84330808 b84330808 changed the title Fix service connections on Windows without PGSYSCONFDIR Look up service files like libpq, fixing a crash on Windows Nov 5, 2026
@b84330808

Copy link
Copy Markdown
Contributor Author

Good catches, thanks! Folded all of it in: the per-user file comes first, then $PGSYSCONFDIR/pg_service.conf (no dot) if the service is not there, and with APPDATA unset there is no per-user file instead of a relative path.

This does change one thing for existing setups: $PGSYSCONFDIR/.pg_service.conf is not read anymore. libpq's compiled-in SYSCONFDIR fallback is not covered either, since pgcli cannot know that path.

This branch has not been deployed

No deployments
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.

2 participants