Which library to query a database (postgreSQL) is popular in OCaml?

I am building an application that will need to persist data in a database. I need to query this database. I don’t mind either the ORM approach or the sql-query writing approach.

I saw a couple of options around, but I wanted to ask and see which option is more popular in the community.

Many of the ones I saw did not seem to be maintained, such as sequoia, ocamlodbc, ocamldbi
Many did not seem to have community backing or support / reviews, such as fun-sql and petrol

The only options I found to have some community support are:

  • pgocaml: seems great, but a bit restrictive. Could not find a way to “compose” queries by building them from strings. More info in this open issue: Conditionally compose / build queries, avoiding repetition? · Issue #125 · darioteixeira/pgocaml · GitHub
  • postgresql-ocaml: second option. Looks like what I need, but I could not find a “getting started” style documentation. the API docs is daunting when all I want to do is run some queries
  • caqti: this seems to be the backend for many of the other libraries, so maybe it is not made to be used directly? I saw some talk about the approach of using it with ppx-rapper. but could not find more info about this approach.

So is there a more popular option in the community that I can find more guides or info about?

2 Likes

We have used this library in production for many years at LexiFi and have never had any problems. As I remember it, the library is just a thin wrapper around the C API of libpq, so one can look at the documentation of the C API to get started.

Cheers,
Nicolas

My favorite combo is Caqti and ppx_rapper.

Caqti is more portable (3 supported databases). And ppx_rapper introduce some nice typing support.

See my page index (caqti_ppx_rapper.index)

Worth mentioning is a 4th option: PGX, which is a pure OCaml client library. I haven’t used it yet, but it’s the first one I intend to try when I get that far.