Skip to content

Commit

Permalink
Add support to handle unix socket in the host option
Browse files Browse the repository at this point in the history
When calling the function pgsql:connect/2, instead of
 {host, "server.example.com"}
you can provide the path to the unix socket like
 {host, "unix:/path/to/socket"}
For example:
 {host, "unix:/var/run/postgresql/.s.PGSQL.5432"}

In that case, there is no need to provide the 'port' option.
  • Loading branch information
badlop committed May 13, 2024
1 parent bc3c581 commit 35550c5
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/pgsql_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,11 @@ connect(Host, Port, Timeout) ->
Err
end.

do_connect([{IP, Family}|AddrsFamilies], Port, Timeout, _Err) ->
do_connect([{IP, Family}|AddrsFamilies], Port1, Timeout, _Err) ->
Port = case Family of
local -> 0;
_ -> Port1
end,
case gen_tcp:connect(IP, Port, [{active, false}, binary,
{packet, raw}, Family], Timeout) of
{ok, Sock} ->
Expand All @@ -419,6 +423,8 @@ do_connect([{IP, Family}|AddrsFamilies], Port, Timeout, _Err) ->
do_connect([], _Port, _Timeout, Err) ->
Err.

lookup([$u,$n,$i,$x,$: | Path], _Timeout) ->
{ok, [{{local, Path}, local}]};
lookup(Host, Timeout) ->
case inet:parse_address(Host) of
{ok, IP} ->
Expand Down

0 comments on commit 35550c5

Please sign in to comment.