diff --git a/docs/connect/erlang/epgsql.md b/docs/connect/erlang/epgsql.md new file mode 100644 index 00000000..dc6d6f07 --- /dev/null +++ b/docs/connect/erlang/epgsql.md @@ -0,0 +1,83 @@ +(erlang-epgsql)= + +# Erlang epgsql + +:::{div} sd-text-muted +Connect to CrateDB from Erlang using epgsql. +::: + +:::{rubric} About +::: + +[epgsql] is the designated Erlang PostgreSQL client library. + +:::{rubric} Synopsis +::: + +`rebar.config` +```erlang +{deps, + [ + {epgsql, ".*", {git, "https://github.com/epgsql/epgsql.git", {tag, "4.8.0"}}} + ]}. +``` +`epgsql_example.erl` +```erlang +-module(epgsql_example). +-export([start/0]). + +start() -> + {ok, C} = epgsql:connect(#{ + host => "localhost", + username => "crate", + password => "crate", + database => "doc", + port => 5432, + timeout => 4000 + }), + {ok, _, Result} = epgsql:squery(C, "SELECT mountain, height FROM sys.summits ORDER BY height DESC LIMIT 3"), + io:fwrite("~p~n", [Result]), + ok = epgsql:close(C), + init:stop(). +``` + +:::{rubric} SSL connection +::: + +Start the Erlang [SSL application] first, +use the `ssl` and `ssl_opts` arguments on `epgsql:connect`, and +replace username, password, and hostname with values matching +your environment. + +Also use this variant to connect to CrateDB Cloud. +```erlang +start() -> + ssl:start(), + {ok, C} = epgsql:connect(#{ + host => "testcluster.cratedb.net", + username => "admin", + password => "password", + database => "doc", + port => 5432, + ssl => true, + ssl_opts => [{verify, verify_none}], + timeout => 4000 + }), +``` + +:::{rubric} Example +::: + +Create the files `rebar.config` and `epgsql_example.erl` including the synopsis code shared above. + +:::{include} ../_cratedb.md +::: +```shell +rebar3 compile +erlc epgsql_example.erl +rebar3 shell --eval 'epgsql_example:start().' +``` + + +[epgsql]: https://github.com/epgsql/epgsql +[SSL application]: https://www.erlang.org/docs/28/apps/ssl/ssl_app.html diff --git a/docs/connect/erlang/index.md b/docs/connect/erlang/index.md new file mode 100644 index 00000000..8555dc2e --- /dev/null +++ b/docs/connect/erlang/index.md @@ -0,0 +1,12 @@ +(connect-erlang)= + +# Erlang + +:::{div} sd-text-muted +Connect to CrateDB from Erlang applications. +::: + +:::{toctree} +odbc +epgsql +::: diff --git a/docs/connect/erlang/odbc.md b/docs/connect/erlang/odbc.md new file mode 100644 index 00000000..03620396 --- /dev/null +++ b/docs/connect/erlang/odbc.md @@ -0,0 +1,72 @@ +(erlang-odbc)= + +# Erlang ODBC + +:::{div} sd-text-muted +Connect to CrateDB from Erlang using ODBC. +::: + +:::{rubric} About +::: + +Erlang includes an [ODBC application] out of the box that provides an +interface to communicate with relational SQL-databases, see also +[Erlang ODBC examples]. + +:::{rubric} Install +::: + +:::{include} ../odbc/install.md +::: + +:::{rubric} Synopsis +::: + +Before running the example, ensure the PostgreSQL ODBC driver is +installed on your system. + +`odbc_example.erl` +```erlang +-module(odbc_example). + +main(_) -> + odbc:start(), + {ok, Ref} = odbc:connect("Driver={PostgreSQL Unicode};Server=localhost;Port=5432;Uid=crate;Pwd=crate", []), + io:fwrite("~p~n", [odbc:sql_query(Ref, "SELECT mountain, height FROM sys.summits ORDER BY height DESC LIMIT 3")]), + init:stop(). +``` + +:::{rubric} CrateDB Cloud +::: + +For connecting to CrateDB Cloud, start the Erlang [SSL application], +add `sslmode=require`, and replace `Server`, `Uid`, and `Pwd` with +values matching your environment. + +`odbc_example.erl` +```erlang +-module(odbc_example). + +main(_) -> + ssl:start(), + odbc:start(), + {ok, Ref} = odbc:connect("Driver={PostgreSQL Unicode};Server=testcluster.cratedb.net;Port=5432;sslmode=require;Uid=admin;Pwd=password", []), + io:fwrite("~p~n", [odbc:sql_query(Ref, "SELECT mountain, height FROM sys.summits ORDER BY height DESC LIMIT 3")]), + init:stop(). +``` + +:::{rubric} Example +::: + +Create the file `odbc_example.erl` including the synopsis code shared above. + +:::{include} ../_cratedb.md +::: +```shell +escript odbc_example.erl +``` + + +[Erlang ODBC examples]: https://www.erlang.org/doc/apps/odbc/getting_started.html +[ODBC application]: https://www.erlang.org/docs/28/apps/odbc/odbc.html +[SSL application]: https://www.erlang.org/docs/28/apps/ssl/ssl_app.html diff --git a/docs/connect/index.md b/docs/connect/index.md index 265c8e26..094ca1e9 100644 --- a/docs/connect/index.md +++ b/docs/connect/index.md @@ -76,6 +76,16 @@ Any (ODBC) Elixir :::: +::::{grid-item-card} +:link: connect-erlang +:link-type: ref +:class-body: sd-fs-1 sd-text-center +:class-footer: sd-fs-5 sd-font-weight-bold +{fab}`erlang` ++++ +Erlang +:::: + ::::{grid-item-card} :link: connect-go :link-type: ref @@ -229,6 +239,7 @@ application :hidden: elixir/index +erlang/index go/index java/index javascript