Getting Started with RethinkDB and the Phoenix Framework — Introduction

I recently spent some time getting RethinkDB setup in a Phoenix app and was surprised at how easy was. I figured i’d share what I learned so that others don’t spin their tires trying to figure it out and can get up an running quickly.

Getting Started

The first thing you need to do is install RethinkDB on your machine. You can do that here. If you are running Windows then you will not be able to install RethinkDB unfortunately. It only works on Unix based machines at the moment.

Once you have RethinkDB installed, you will need to start the RethinkDB server. This can be done by either running the launch commands provided by Homebrew (if you installed on a mac) or by running the rethinkdb command in a terminal window. After you run the command open your browser to http://localhost:8080 and you should see this screen:

Congratulations, you now have RethinkDB installed and working! Let’s setup a database and table to get us started. Navigate to the Tables page by clicking on the link at the top of the RethinkDB admin panel. Click the “Add Database” button and add a database called “test”. Note: there may already be a test database created, and if so, you are good to go.

Our next step is to add a table to the “test” database. Click the “Add Table” button and add a table called “posts”.

You can create databases and tables programmatically, but the RethinkDB admin interface is so nice that I like to just do it there.

Adding RethinkDB to Phoenix

The Elixir community is fortunate to have wonderful people like Peter Hamilton who have already done a bunch of work to make RethinkDB a pleasure to work with. All we have to do to add RethinkDB to a new Phoenix app is add the package.

Add the RethinkDB Elixir package to your mix.exs file:

Now, run mix deps.get and the package will be installed.

After you install the package, you are pretty much good to go. However, I like to add abstractions on top of the package code to make it a little easier to work with. Add a database.ex file to your lib folder:

Let’s fire up IEX and try out our new RethinkDB package:

We first need to import our database module and the RethinkDB Query modules before we can work with it:

Now we are ready to fire up a connection to the database:

Our abstraction let’s us pass the name of the database to the init() function in order to start a new connection to it. We created the “test” database earlier so we can pass :test to init() in order to start a connection to it.

We’ve got a connection setup so let’s take it for a spin:

We haven’t added any posts to our posts table so lets do that now:

Let’s now grab our posts table again:

Cool huh? It’s super easy to work with RethinkDB. Let’s try adding more than one post at a time:

We just created a list of post maps. We can insert them into our database like so:

Now, let’s query the “posts” table again and we will filter the results:

After we are done working with a connection we can simply run:

Next Steps

This tutorial works with RethinkDB via the terminal for ease and convenience, however you likely want to work with it in your application code. Fortunately, there is no difference between what we were just doing and working with it in your application code. You first need to import the RethinkDB.Query module wherever you want to read/write to your database and then can work with RethinkDB just like we were (Although, if you want to use the init() , run() and stop() functions you’ll need to add an abstraction module like I did and import it along with RethinkDB.Query ).

If you’d like to learn more about RethinkDB I think this Pluralsight course is excellent.


As you can see, working with RethinkDB is super easy in Phoenix. I think RethinkDB is an excellent database and if you combine it with something like React, Redux, and Phoenix Channels you will be able to build some amazing applications (here is a starter template I created for getting started with Phoenix and React — it is setup for Postgres)

I hope this tutorial was helpful and if you have any questions please feel free to leave me a comment and I’d be happy to help!

Originally published at on November 28, 2015.

Like what you read? Give Ryan Swapp a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.