| 
6 | 6 | Connect to CrateDB from Erlang applications.  | 
7 | 7 | :::  | 
8 | 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 |  | -::::{todo}  | 
19 |  | -Enable this include with the [ODBC patch](https://github.com/crate/cratedb-guide/pull/411).  | 
20 |  | -```md  | 
21 |  | -:::{include} _odbc-setup-widget.md  | 
22 |  | -:::  | 
23 |  | -```  | 
24 |  | -::::  | 
25 |  | - | 
26 |  | -:::{rubric} Synopsis  | 
27 |  | -:::  | 
28 |  | - | 
29 |  | -Before running the example, ensure the PostgreSQL ODBC driver is  | 
30 |  | -installed on your system.  | 
31 |  | - | 
32 |  | -`odbc_example.erl`  | 
33 |  | -```erlang  | 
34 |  | --module(odbc_example).  | 
35 |  | - | 
36 |  | -main(_) ->  | 
37 |  | -    odbc:start(),  | 
38 |  | -    {ok, Ref} = odbc:connect("Driver={PostgreSQL ODBC};Server=localhost;Port=5432;Uid=crate;Pwd=crate", []),  | 
39 |  | -    io:fwrite("~p~n", [odbc:sql_query(Ref, "SELECT mountain, height FROM sys.summits ORDER BY height DESC LIMIT 3")]),  | 
40 |  | -    init:stop().  | 
41 |  | -```  | 
42 |  | - | 
43 |  | -:::{include} ../_cratedb.md  | 
44 |  | -:::  | 
45 |  | -```shell  | 
46 |  | -escript odbc_example.erl  | 
47 |  | -```  | 
48 |  | - | 
49 |  | -:::{rubric} CrateDB Cloud  | 
50 |  | -:::  | 
51 |  | - | 
52 |  | -For connecting to CrateDB Cloud, start the Erlang [SSL application],  | 
53 |  | -add `sslmode=require`, and replace `Server`, `Uid`, and `Pwd` with  | 
54 |  | -values matching your environment.  | 
55 |  | - | 
56 |  | -`odbc_example.erl`  | 
57 |  | -```erlang  | 
58 |  | --module(odbc_example).  | 
59 |  | - | 
60 |  | -main(_) ->  | 
61 |  | -    ssl:start(),  | 
62 |  | -    odbc:start(),  | 
63 |  | -    {ok, Ref} = odbc:connect("Driver={PostgreSQL};Server=testcluster.cratedb.net;Port=5432;sslmode=require;Uid=admin;Pwd=password", []),  | 
64 |  | -    io:fwrite("~p~n", [odbc:sql_query(Ref, "SELECT mountain, height FROM sys.summits ORDER BY height DESC LIMIT 3")]),  | 
65 |  | -    init:stop().  | 
66 |  | -```  | 
67 |  | - | 
68 |  | - | 
69 |  | -## epgsql  | 
70 |  | - | 
71 |  | -[epgsql] is the designated Erlang PostgreSQL client library.   | 
72 |  | - | 
73 |  | -`rebar.config`  | 
74 |  | -```erlang  | 
75 |  | -{deps,  | 
76 |  | - [  | 
77 |  | -  {epgsql, ".*", {git, "https://github.com/epgsql/epgsql.git", {tag, "4.8.0"}}}  | 
78 |  | - ]}.  | 
79 |  | -```  | 
80 |  | -`epgsql_example.erl`  | 
81 |  | -```erlang  | 
82 |  | --module(epgsql_example).  | 
83 |  | --export([start/0]).  | 
84 |  | - | 
85 |  | -start() ->  | 
86 |  | -    {ok, C} = epgsql:connect(#{  | 
87 |  | -        host => "localhost",  | 
88 |  | -        username => "crate",  | 
89 |  | -        password => "crate",  | 
90 |  | -        database => "doc",  | 
91 |  | -        port => 5432,  | 
92 |  | -        timeout => 4000  | 
93 |  | -    }),  | 
94 |  | -    {ok, _, Result} = epgsql:squery(C, "SELECT mountain, height FROM sys.summits ORDER BY height DESC LIMIT 3"),  | 
95 |  | -    io:fwrite("~p~n", [Result]),  | 
96 |  | -    ok = epgsql:close(C),  | 
97 |  | -    init:stop().  | 
98 |  | -```  | 
99 |  | - | 
100 |  | -:::{include} ../_cratedb.md  | 
101 |  | -:::  | 
102 |  | -```shell  | 
103 |  | -rebar3 compile  | 
104 |  | -erlc epgsql_example.erl  | 
105 |  | -rebar3 shell --eval 'epgsql_example:start().'  | 
106 |  | -```  | 
107 |  | - | 
108 |  | -:::{rubric} CrateDB Cloud  | 
 | 9 | +:::{toctree}  | 
 | 10 | +odbc  | 
 | 11 | +epgsql  | 
109 | 12 | :::  | 
110 |  | - | 
111 |  | -For connecting to CrateDB Cloud, start the Erlang [SSL application] first,  | 
112 |  | -use the `ssl` and `ssl_opts` arguments on `epgsql:connect`, and  | 
113 |  | -replace username, password, and hostname with values matching  | 
114 |  | -your environment.  | 
115 |  | -```erlang  | 
116 |  | -start() ->  | 
117 |  | -    ssl:start(),  | 
118 |  | -    {ok, C} = epgsql:connect(#{  | 
119 |  | -        host => "testcluster.cratedb.net",  | 
120 |  | -        username => "admin",  | 
121 |  | -        password => "password",  | 
122 |  | -        database => "doc",  | 
123 |  | -        port => 5432,  | 
124 |  | -        ssl => true,  | 
125 |  | -        ssl_opts => [{verify, verify_none}],  | 
126 |  | -        timeout => 4000  | 
127 |  | -    }),  | 
128 |  | -```  | 
129 |  | - | 
130 |  | - | 
131 |  | -[epgsql]: https://github.com/epgsql/epgsql  | 
132 |  | -[Erlang ODBC examples]: https://www.erlang.org/doc/apps/odbc/getting_started.html  | 
133 |  | -[ODBC application]: https://www.erlang.org/docs/28/apps/odbc/odbc.html  | 
134 |  | -[SSL application]: https://www.erlang.org/docs/28/apps/ssl/ssl_app.html  | 
0 commit comments