analytics
analytics copied to clipboard
PostgreSQL query-string connection parameters in `DATABASE_URL` are not respected when using a Unix domain socket.
Past Issues Searched
- [x] I have searched open and closed issues to make sure that the bug has not yet been reported
Issue is a Bug Report
- [x] This is a bug report and not a feature request, nor asking for self-hosted support
Using official Plausible Cloud hosting or self-hosting?
Self-hosting
Describe the bug
I’ve had a hard time troubleshooting this. The current logic for parsing DATABASE_URL, the postgres(ql):// connection URL, is incomplete.
Currently,
- PostgreSQL query-string connection parameters in
DATABASE_URL,?…=…&…=…, are not respected when using a Unix domain socket. - You cannot have Plausible use a Unix domain socket file other than
.s.PGSQL.5432.
The current URL parsing logic
https://github.com/plausible/analytics/blob/e7e553cf9d27991c640bc52ef797297435aac0a2/config/runtime.exs#L483-L508
- If the
hostpart of the URL begins with%2F(uppercase) or is empty:- It takes the
socket_dirfrom thehostpart or thehostquery-string parameter. - CAVEAT:
- Other query-string parameters, including
application_name,search_path, et cetera, are ignored. Currently, I’m specifyingpostgresql://user:password@%2FPostgreSQL%2Fsocket%2F/database?application_name=Plausiblefor myDATABASE_URL, but theapplication_nameparameter doesn’t make it into the PostgreSQL server log. - The
port, either in theportpart (:…) or in the query-string, is ignored. It should not, becauseportdetermines the suffix of the socket filename. For example,port = 1inpostgresql.confcreates the Unix domain socket.s.PGSQL.1, not.s.PGSQL.5432, the default. I had to comment outport = 1in mypostgresql.conffor this reason.
- Other query-string parameters, including
- It takes the
- Otherwise:
- The full URL gets passed (
config :plausible, Plausible.Repo, url: db_url) to the underlying driver, Postgrex. The query-string parameters would be respected. - CAVEAT:
- Postgrex does not like
%2f/%2Fin thehostpart, emitting the errornon-existing domain - :nxdomain. It means you cannot specify a Unix domain socket for thehost. - The clever trickery of having the
hostquery-string parameter override thehostpart of the URL, e.g.postgresql://user:password@fakehost/database?host=%2FPostgreSQL%2Fsocket%2F, could not establish the connection, either.
- Postgrex does not like
- The full URL gets passed (
Expected behavior
- PostgreSQL query-string connection parameters in
DATABASE_URLshould be respected in any case. - It should allow specifying
portfor the Unix domain socket filename suffix.
Screenshots
No response
Environment
The Docker image `ghcr.io/plausible/community-edition:v3.0.1`