|
| 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} Prerequisites |
| 19 | +::: |
| 20 | + |
| 21 | +Install ODBC driver. |
| 22 | + |
| 23 | +`odbcinst.ini` |
| 24 | +```ini |
| 25 | +[PostgreSQL] |
| 26 | +Description = PostgreSQL ODBC driver |
| 27 | +Driver = /usr/local/lib/psqlodbcw.so |
| 28 | +``` |
| 29 | +```shell |
| 30 | +odbcinst -i -d -f odbcinst.ini |
| 31 | +``` |
| 32 | + |
| 33 | +:::{rubric} Synopsis |
| 34 | +::: |
| 35 | + |
| 36 | +`odbc_example.erl` |
| 37 | +```erlang |
| 38 | +-module(odbc_example). |
| 39 | + |
| 40 | +main(_) -> |
| 41 | + odbc:start(), |
| 42 | + {ok, Ref} = odbc:connect("Driver={PostgreSQL};Servername=localhost;Portnumber=5432;Uid=crate;Pwd=crate", []), |
| 43 | + io:fwrite("~p~n", [odbc:sql_query(Ref, "SELECT mountain, height FROM sys.summits ORDER BY height DESC LIMIT 3")]), |
| 44 | + init:stop(). |
| 45 | +``` |
| 46 | + |
| 47 | +Start CrateDB using Docker or Podman, then compile and invoke example program. |
| 48 | +```shell |
| 49 | +docker run --rm --publish=5432:5432 crate -Cdiscovery.type=single-node |
| 50 | +``` |
| 51 | +```shell |
| 52 | +escript odbc_example.erl |
| 53 | +``` |
| 54 | + |
| 55 | + |
| 56 | +## epgsql |
| 57 | + |
| 58 | +[epgsql] is the designated Erlang PostgreSQL client library. |
| 59 | + |
| 60 | +`rebar.config` |
| 61 | +```erlang |
| 62 | +{deps, |
| 63 | + [ |
| 64 | + {epgsql, ".*", {git, "https://github.com/epgsql/epgsql.git", {tag, "4.8.0"}}} |
| 65 | + ]}. |
| 66 | +``` |
| 67 | +`epgsql_example.erl` |
| 68 | +```erlang |
| 69 | +-module(epgsql_example). |
| 70 | +-export([start/0]). |
| 71 | + |
| 72 | +start() -> |
| 73 | + {ok, C} = epgsql:connect(#{ |
| 74 | + host => "localhost", |
| 75 | + username => "crate", |
| 76 | + password => "crate", |
| 77 | + database => "doc", |
| 78 | + port => 5432, |
| 79 | + timeout => 4000 |
| 80 | + }), |
| 81 | + {ok, _, Result} = epgsql:squery(C, "SELECT mountain, height FROM sys.summits ORDER BY height DESC LIMIT 3"), |
| 82 | + io:fwrite("~p~n", [Result]), |
| 83 | + ok = epgsql:close(C), |
| 84 | + init:stop(). |
| 85 | +``` |
| 86 | + |
| 87 | +Start CrateDB using Docker or Podman, then compile and invoke example program. |
| 88 | +```shell |
| 89 | +docker run --rm --publish=5432:5432 crate -Cdiscovery.type=single-node |
| 90 | +``` |
| 91 | +```shell |
| 92 | +rebar3 compile |
| 93 | +erlc epgsql_example.erl |
| 94 | +erl -pa ebin ./_build/default/lib/epgsql/ebin ./_build/default/lib/epgsql/include -noshell -run epgsql_example |
| 95 | +``` |
| 96 | +Stop the REPL using `CTRL+G`, `q`, `ENTER`. |
| 97 | + |
| 98 | +:::{rubric} CrateDB Cloud |
| 99 | +::: |
| 100 | + |
| 101 | +For connecting to CrateDB Cloud, start the Erlang [SSL application] first, |
| 102 | +use the `ssl` and `ssl_opts` arguments on `epgsql:connect`, and |
| 103 | +replace username, password, and hostname with values matching |
| 104 | +your environment. |
| 105 | +```erlang |
| 106 | +start() -> |
| 107 | + ssl:start(), |
| 108 | + {ok, C} = epgsql:connect(#{ |
| 109 | + host => "testcluster.cratedb.net", |
| 110 | + username => "admin", |
| 111 | + password => "password", |
| 112 | + database => "doc", |
| 113 | + port => 5432, |
| 114 | + ssl => true, |
| 115 | + ssl_opts => [{verify, verify_none}], |
| 116 | + timeout => 4000 |
| 117 | + }), |
| 118 | +``` |
| 119 | + |
| 120 | + |
| 121 | +[epgsql]: https://github.com/epgsql/epgsql |
| 122 | +[Erlang ODBC examples]: https://www.erlang.org/doc/apps/odbc/getting_started.html |
| 123 | +[ODBC application]: https://www.erlang.org/docs/28/apps/odbc/odbc.html |
| 124 | +[SSL application]: https://www.erlang.org/docs/28/apps/ssl/ssl_app.html |
0 commit comments