ElixirΒΆ
Connect to CrateDB from Elixir applications.
About
Postgrex is the canonical PostgreSQL driver for Elixir.
Synopsis
mix.exs
defmodule CrateDbExample do
use Mix.Project
def project do
[
app: :cratedb_elixir_example,
version: "0.0.0",
deps: [{:postgrex, "~> 0.21.0"}],
]
end
end
example.exs
options = [
hostname: "localhost",
port: 5432,
ssl: false,
username: "crate",
password: "crate",
backoff_type: :stop,
max_restarts: 0,
show_sensitive_data_on_connection_error: true,
]
{:ok, conn} = Postgrex.start_link(options)
result = Postgrex.query!(conn, "SELECT region, mountain, height FROM sys.summits ORDER BY height DESC LIMIT 5", [])
IO.inspect(result)
SSL connection
Use the ssl: true parameter, and replace username, password,
and hostname with values matching your environment.
Also use this variant to connect to CrateDB Cloud.
options = [
hostname: "testcluster.cratedb.net",
port: 5432,
ssl: true,
username: "admin",
password: "password",
backoff_type: :stop,
max_restarts: 0,
show_sensitive_data_on_connection_error: true,
]
Quickstart example
Create the files mix.exs and example.exs including the synopsis code shared above.
Start CrateDB using Docker or Podman, then invoke the example program.
docker run --rm --publish=5432:5432 docker.io/crate '-Cdiscovery.type=single-node'
mix deps.get
mix run example.exs
Full example
Connect to CrateDB and CrateDB Cloud using Elixir.