| 
 | 1 | +(connect-erlang)=  | 
 | 2 | + | 
 | 3 | +# Erlang  | 
 | 4 | + | 
 | 5 | +:::{div} sd-text-muted  | 
 | 6 | +Connect to CrateDB from Erlang applications.  | 
 | 7 | +:::  | 
 | 8 | + | 
 | 9 | +## ODBC  | 
 | 10 | + | 
 | 11 | +:::{rubric} About  | 
 | 12 | +:::  | 
 | 13 | + | 
 | 14 | +Erlang includes an [ODBC application] out of the box that provides an  | 
 | 15 | +interface to communicate with relational SQL-databases, see also  | 
 | 16 | +[Erlang ODBC examples].  | 
 | 17 | + | 
 | 18 | +:::{rubric} Synopsis  | 
 | 19 | +:::  | 
 | 20 | + | 
 | 21 | +`odbcinst.ini`  | 
 | 22 | +```ini  | 
 | 23 | +[PostgreSQL]  | 
 | 24 | +Description     = PostgreSQL ODBC driver  | 
 | 25 | +Driver          = /usr/local/lib/psqlodbcw.so  | 
 | 26 | +```  | 
 | 27 | +```shell  | 
 | 28 | +odbcinst -i -d -f odbcinst.ini  | 
 | 29 | +```  | 
 | 30 | +`odbc_demo.erl`  | 
 | 31 | +```erlang  | 
 | 32 | +-module(odbc_demo).  | 
 | 33 | +-export([start/0]).  | 
 | 34 | + | 
 | 35 | +start() ->  | 
 | 36 | +    odbc:start(),  | 
 | 37 | +    {ok, Ref} = odbc:connect("Driver={PostgreSQL};Servername=localhost;Portnumber=5432;Uid=crate;Pwd=crate", []),  | 
 | 38 | +    io:fwrite("~p", [odbc:sql_query(Ref, "SELECT mountain, height FROM sys.summits ORDER BY height DESC LIMIT 3")]).  | 
 | 39 | +```  | 
 | 40 | +```shell  | 
 | 41 | +erlc odbc_demo.erl && erl -s odbc_demo  | 
 | 42 | +```  | 
 | 43 | + | 
 | 44 | +## epgsql  | 
 | 45 | + | 
 | 46 | +[epgsql] is the designated Erlang PostgreSQL client library.   | 
 | 47 | + | 
 | 48 | +`rebar.config`  | 
 | 49 | +```erlang  | 
 | 50 | +{deps,  | 
 | 51 | + [  | 
 | 52 | +  {epgsql, ".*", {git, "https://github.com/epgsql/epgsql.git", {tag, "4.8.0"}}}  | 
 | 53 | + ]}.  | 
 | 54 | +```  | 
 | 55 | +```shell  | 
 | 56 | +rebar3 compile  | 
 | 57 | +```  | 
 | 58 | +`epgsql_demo.erl`  | 
 | 59 | +```erlang  | 
 | 60 | +-module(epgsql_demo).  | 
 | 61 | +-export([start/0]).  | 
 | 62 | + | 
 | 63 | +start() ->  | 
 | 64 | +    {ok, C} = epgsql:connect(#{  | 
 | 65 | +        host => "localhost",  | 
 | 66 | +        username => "crate",  | 
 | 67 | +        password => "crate",  | 
 | 68 | +        database => "doc",  | 
 | 69 | +        port => 5432,  | 
 | 70 | +        timeout => 4000  | 
 | 71 | +    }),  | 
 | 72 | +    {ok, _, Result} = epgsql:squery(C, "SELECT mountain, height FROM sys.summits ORDER BY height DESC LIMIT 3"),  | 
 | 73 | +    io:fwrite("~p", [Result]),  | 
 | 74 | +    ok = epgsql:close(C).  | 
 | 75 | +```  | 
 | 76 | +```shell  | 
 | 77 | +erlc epgsql_demo.erl  | 
 | 78 | +erl -pa ebin ./_build/default/lib/epgsql/ebin ./_build/default/lib/epgsql/include -s epgsql_demo  | 
 | 79 | +```  | 
 | 80 | + | 
 | 81 | + | 
 | 82 | +[epgsql]: https://github.com/epgsql/epgsql  | 
 | 83 | +[Erlang ODBC examples]: https://www.erlang.org/doc/apps/odbc/getting_started.html  | 
 | 84 | +[ODBC application]: https://www.erlang.org/doc/apps/odbc/odbc.html  | 
0 commit comments