|
| 1 | +(connect-elixir)= |
| 2 | + |
| 3 | +# Elixir |
| 4 | + |
| 5 | +:::{div} sd-text-muted |
| 6 | +Connect to CrateDB from Elixir applications. |
| 7 | +::: |
| 8 | + |
| 9 | +:::{rubric} About |
| 10 | +::: |
| 11 | + |
| 12 | +[Postgrex] is the canonical PostgreSQL driver for Elixir. |
| 13 | + |
| 14 | +:::{rubric} Synopsis |
| 15 | +::: |
| 16 | + |
| 17 | +`mix.exs` |
| 18 | +```elixir |
| 19 | +defmodule CrateDbExample do |
| 20 | + use Mix.Project |
| 21 | + def project do |
| 22 | + [ |
| 23 | + app: :cratedb_elixir_example, |
| 24 | + version: "0.0.0", |
| 25 | + deps: [{:postgrex, "~> 0.21.0"}], |
| 26 | + ] |
| 27 | + end |
| 28 | +end |
| 29 | +``` |
| 30 | +`example.exs` |
| 31 | +```elixir |
| 32 | +options = [ |
| 33 | + hostname: "localhost", |
| 34 | + port: 5432, |
| 35 | + ssl: false, |
| 36 | + username: "crate", |
| 37 | + password: "crate", |
| 38 | + database: "doc", |
| 39 | + backoff_type: :stop, |
| 40 | + max_restarts: 0, |
| 41 | + show_sensitive_data_on_connection_error: true, |
| 42 | +] |
| 43 | + |
| 44 | +{:ok, conn} = Postgrex.start_link(options) |
| 45 | +result = Postgrex.query!(conn, "SELECT region, mountain, height FROM sys.summits ORDER BY height DESC LIMIT 5", []) |
| 46 | +IO.inspect(result) |
| 47 | +``` |
| 48 | + |
| 49 | +:::{include} ../_cratedb.md |
| 50 | +::: |
| 51 | +```shell |
| 52 | +mix deps.get |
| 53 | +mix run example.exs |
| 54 | +``` |
| 55 | + |
| 56 | +:::{rubric} CrateDB Cloud |
| 57 | +::: |
| 58 | + |
| 59 | +For connecting to CrateDB Cloud, adjust the `ssl: true` parameter, |
| 60 | +and replace hostname, username, and password with values matching your |
| 61 | +environment. |
| 62 | +```elixir |
| 63 | +options = [ |
| 64 | + hostname: "testcluster.cratedb.net", |
| 65 | + port: 5432, |
| 66 | + ssl: true, |
| 67 | + username: "admin", |
| 68 | + password: "password", |
| 69 | + database: "doc", |
| 70 | + backoff_type: :stop, |
| 71 | + max_restarts: 0, |
| 72 | + show_sensitive_data_on_connection_error: true, |
| 73 | +] |
| 74 | +``` |
| 75 | + |
| 76 | +## Example |
| 77 | + |
| 78 | +:::{card} |
| 79 | +:link: https://github.com/crate/cratedb-examples/tree/main/by-language/elixir-postgrex |
| 80 | +:link-type: url |
| 81 | +{material-outlined}`play_arrow;2em` |
| 82 | +Connect to CrateDB and CrateDB Cloud using Elixir. |
| 83 | ++++ |
| 84 | +Demonstrates a basic example that uses the Postgrex driver. |
| 85 | +::: |
| 86 | + |
| 87 | +[](https://github.com/crate/cratedb-examples/actions/workflows/lang-elixir-postgrex.yml) |
| 88 | + |
| 89 | + |
| 90 | +[Postgrex]: https://hexdocs.pm/postgrex/readme.html |
0 commit comments