Skip to content

docs: Explain the default socket directory of dbConnect() - #673

Merged
krlmlr merged 5 commits into
mainfrom
claude/fix-564-socket-dir
Nov 29, 2026
Merged

krlmlr merged 5 commits into
mainfrom
claude/fix-564-socket-dir

Conversation

@krlmlr

@krlmlr krlmlr commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

Closes #564.

Analysis

When neither host, PGHOST, nor a service sets the host, libpq connects to a Unix-domain socket in DEFAULT_PGSOCKET_DIR, a compile-time constant of the libpq that is loaded at run time (on Windows, it connects to localhost). RPostgres does not choose it: on Linux, configure links -lpq dynamically against whatever pkg-config/pg_config finds; nothing is bundled or downloaded.

  • Upstream PostgreSQL 16.15 sets #define DEFAULT_PGSOCKET_DIR "/tmp" in src/include/pg_config_manual.h.
  • Debian/Ubuntu patch that to /var/run/postgresql (debian/patches/51-default-sockets-in-var.patch, "Put server Unix sockets into /var/run/postgresql/ by default"). Checked in the Ubuntu noble postgresql-16 source package; the Debian bookworm archive was not reachable from the session, but the patch is Debian's own.
  • Binary builds from Posit Package Manager for noble and bookworm (RPostgres 1.4.10) link libpq.so.5 dynamically (objdump -p: NEEDED libpq.so.5), so they inherit the system default.

Reproduction on Ubuntu 24.04 with a PostgreSQL 16 server whose socket is in /var/run/postgresql:

  • P3M binary, system libpq: dbConnect(Postgres()) connects, host /var/run/postgresql.
  • RPostgres built from source against libpq-dev: same.
  • The same P3M binary, unchanged, with an upstream libpq 16.15 built from source found first on the library path: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory, the exact error from the issue; postgresHasDefault() is FALSE. With host = "/var/run/postgresql" or PGHOST=/var/run/postgresql, it connects.

So a Debian user sees /tmp whenever the libpq that ends up loaded is not Debian's: a source install of RPostgres whose pkg-config/pg_config points at a non-Debian libpq (PostgreSQL built from source, conda, Homebrew on Linux), or a non-Debian libpq.so.5 that precedes the system one on the run-time library path (R's etc/ldpaths in the Posit/rig builds puts /usr/local/lib before /usr/lib/x86_64-linux-gnu). Which of these applied to the reporter is not known. The conda-forge and Homebrew defaults were not checked in this session.

Change

Documentation only: a new "Default host" section in ?Postgres explains where the default comes from (localhost on Windows; on Unix-like systems a socket directory compiled into libpq, /var/run/postgresql in some distributions such as Debian and Ubuntu, /tmp upstream), the error it produces, and the two workarounds (host = "/var/run/postgresql" or PGHOST), and points to postgresHasDefault().

?Redshift is unchanged (man/Redshift.Rd is identical to main): Redshift clusters are reached over TCP at their endpoint, so Unix-domain sockets do not apply. The text therefore lives in a Postgres-only section instead of the shared host parameter.

dbConnect() itself stays unchanged: probing several socket directories would make it depend on the file system and could silently connect to a different server. This follows the stance stated on #553 that dbConnect() should be fully deterministic and that only postgresDefault()/postgresHasDefault() should change. #553 (in progress on copilot/align-postgres-default-settings) changes those helpers and their error message; this PR deliberately leaves R/default.R alone to avoid conflicts. The options are posted as a question.

No tests: the change is documentation only.

Clean review: no findings above Low; two wording fixes applied. Accepted limitations.

https://claude.ai/code/session_01Hr1rd6e5NcxC9GSrNJDokc


Generated by Claude Code

Without a host, libpq connects to the socket directory compiled into the libpq loaded at run time:
/var/run/postgresql for Debian and Ubuntu's libpq, /tmp for upstream builds.
Document this and the workarounds (`host` or `PGHOST`).

Closes #564.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd6e5NcxC9GSrNJDokc

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Question: How should RPostgres handle a libpq whose default socket directory is /tmp while the server's socket is in /var/run/postgresql (#564)?

Situation: the socket directory used when no host is given is compiled into the libpq loaded at run time (/tmp upstream, /var/run/postgresql in Debian/Ubuntu). The reporter's error comes from a non-Debian libpq being loaded; this PR reproduces it with an unchanged P3M binary once an upstream-built libpq is first on the library path. RPostgres links libpq dynamically on Linux, so there is no build-time switch in this package to fix. The PR currently only documents the cause and the workarounds (diff). Your earlier statement on #553 says dbConnect() should stay deterministic and only postgresDefault()/postgresHasDefault() should change; #553 is being worked on separately.

You decide. If nobody decides, the PR stays as documentation only and waits for review.


Generated by Claude Code

krlmlr commented Nov 29, 2026 •

Copy link
Copy Markdown
Member Author

Superseded: Accepted limitations

Superseded by #673 (comment).

  • R/dbConnect_PqDriver.R#L35-L36: the quoted error text, connection to server on socket "/tmp/.s.PGSQL.5432" failed, is the wording of libpq 14 and later; older libpq words it differently. It is introduced with "such as", so the docs do not promise exact text. Relevance: low; reviewer confidence: high.
  • R/dbConnect_PqDriver.R#L37-L38: the docs do not mention that libpq 10 and later accept a comma-separated list of hosts, e.g. host = "/var/run/postgresql,/tmp", which would cover both socket directories. Left out to keep the advice simple. Relevance: low; reviewer confidence: high.

Generated by Claude Code

@krlmlr
krlmlr marked this pull request as ready for review November 29, 2026 19:22
@krlmlr

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

?Redshift shouldn't change, no socket connection supported IIUC?

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Confirmed: Redshift clusters are reached over TCP only, so Unix sockets do not apply.
?Redshift is now byte-identical to main again.
Commit 4b306ec moves the socket-directory text from the shared host parameter into a Postgres-only "Default host" section of ?Postgres.


Generated by Claude Code

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Accepted limitations

  • R/dbConnect_PqDriver.R#L35-L36: the quoted error text, connection to server on socket "/tmp/.s.PGSQL.5432" failed, is the wording of libpq 14 and later; older libpq words it differently. It is introduced with "such as", so the docs do not promise exact text. Relevance: low; reviewer confidence: high.
  • R/dbConnect_PqDriver.R#L37-L38: the docs do not mention that libpq 10 and later accept a comma-separated list of hosts, e.g. host = "/var/run/postgresql,/tmp", which would cover both socket directories. Left out to keep the advice simple. Relevance: low; reviewer confidence: high.

Generated by Claude Code

@krlmlr

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Also resolve conflicts.

…-dir

Keep both the "Default host" section from this branch and the "Schemas" section from #675 in ?Postgres.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd6e5NcxC9GSrNJDokc
@krlmlr
krlmlr enabled auto-merge (squash) November 29, 2026 19:59
@krlmlr
krlmlr merged commit 010b0f3 into main Nov 29, 2026
6 of 7 checks passed
@krlmlr
krlmlr deleted the claude/fix-564-socket-dir branch November 29, 2026 20:04
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.

Error in dbConnect(): linux socket location moved

2 participants