Erlang epgsqlΒΆ
Connect to CrateDB from Erlang using epgsql.
About
epgsql is the designated Erlang PostgreSQL client library.
Synopsis
rebar.config
{deps,
[
{epgsql, ".*", {git, "https://github.com/epgsql/epgsql.git", {tag, "4.8.0"}}}
]}.
epgsql_example.erl
-module(epgsql_example).
-export([start/0]).
start() ->
{ok, C} = epgsql:connect(#{
host => "localhost",
username => "crate",
password => "crate",
port => 5432,
ssl => false,
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().
SSL connection
Start the Erlang SSL application,
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.
start() ->
ssl:start(),
{ok, C} = epgsql:connect(#{
host => "testcluster.cratedb.net",
username => "admin",
password => "password",
port => 5432,
ssl => true,
ssl_opts => [{verify, verify_none}],
timeout => 4000
}),
Example
Create the files rebar.config and epgsql_example.erl 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'
rebar3 compile
erlc epgsql_example.erl
rebar3 shell --eval 'epgsql_example:start().'