Skip to content

Commit

Permalink
bugfix: treat PGHOST env var as directory properly
Browse files Browse the repository at this point in the history
This change makes the driver find the actual socket file and is
consistent with how libpq appends .s.PGSQL.5432 to the value of the env
var when it's a path:

    ❯ mkdir -p /tmp/foo && PGHOST=/tmp/foo psql
    psql: error: could not connect to server: No such file or directory
            Is the server running locally and accepting
            connections on Unix domain socket "/tmp/foo/.s.PGSQL.5432"?
  • Loading branch information
will committed May 31, 2023
1 parent cafe2f8 commit cafe5bf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
6 changes: 6 additions & 0 deletions spec/pq/conninfo_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,11 @@ describe PQ::ConnInfo, ".from_conninfo_string" do
ci.port.should eq(1)
ci.user.should eq("D")
end

env_var_bubble do
ENV["PGHOST"] = "/path"
ci = PQ::ConnInfo.from_conninfo_string("postgres://")
ci.host.should eq("/path/.s.PGSQL.5432")
end
end
end
4 changes: 3 additions & 1 deletion src/pq/conninfo.cr
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ module PQ
private def default_host(h)
return h if h && !h.blank?

return ENV["PGHOST"] if ENV.has_key?("PGHOST")
if pghost = ENV["PGHOST"]?
return pghost[0] == '/' ? "#{pghost}/.s.PGSQL.5432" : pghost
end

SOCKET_SEARCH.each do |s|
return s if File.exists?(s)
Expand Down

0 comments on commit cafe5bf

Please sign in to comment.