Skip to content

A native Postgres driver for OCaml 5 and Eio.

Written for Eio in OCaml, the protocol spoken without libpq.

Terminal window
opam repo add hypha https://github.com/hyphatech/opam-repository.git
opam install postgres-eio
let () =
Eio_main.run @@ fun env ->
Eio.Switch.run @@ fun sw ->
match Postgres_eio.Conninfo.of_string "postgres://app@localhost/shop" with
| Error e -> prerr_endline e
| Ok conninfo -> (
match
Postgres_eio.connect ~sw ~net:(Eio.Stdenv.net env)
~mono_clock:(Eio.Stdenv.mono_clock env)
conninfo
with
| Error e -> prerr_endline (Postgres_eio.error_to_string e)
| Ok conn ->
(match
Postgres_eio.query conn
"select name from customers where country = $1"
~params:[ Some "GB" ] ~init:[] ~row:(fun names row ->
match row with [| Some name |] -> name :: names | _ -> names)
with
| Ok (names, _tag) -> List.iter print_endline names
| Error e -> prerr_endline (Postgres_eio.error_to_string e));
Postgres_eio.close conn)

Secure by default

TLS, SCRAM-SHA-256 with channel binding and client certificates; a connection that crosses a network is encrypted unless its string says otherwise.

Pool and failover

One pool serves every domain, across several hosts, with failover and load balancing.

Streams

COPY in and out, and rows folded as they arrive, never buffered.

LISTEN/NOTIFY

A listener that reconnects by itself and says so.

Failures you can act on

Each says whether the statement may have run, so a retry is never blind.