DEV Community

Lucas Barret
Lucas Barret

Posted on • Updated on

Deploy Rails with Yugabyte

So recently, I've discussed this with Franck Pachot, about databases. And he told me about YugabyteDB and the advantages of distributed SQL.

This article will show how to deploy a Yugabyte database locally. And then connect a Rails app to it.

Let's see how smoothly this is!

Let's deploy this locally

The documentation is well explained to deploy your Yugabyte database locally.

I created a docker-compose.yml to avoid rerunning the same command over and over.

services: 
  yuga:
    image: "yugabytedb/yugabyte:2.17.3.0-b152"
    ports: 
      - "7001:7000"
      - "9042:9042"
      - "5433:5433"
      - "9000:9000"
    command: "bin/yugabyted start --daemon=false" 
Enter fullscreen mode Exit fullscreen mode

Here you go. Now you can try to connect to the database :
In the documentation, they said that you could connect to the YugabyteDB with this command :
By running

docker exec -it yugabyte-yuga-1 /home/yugabyte/bin/ysqlsh --echo-queries
Enter fullscreen mode Exit fullscreen mode

But it turns out that I get this error :

ysqlsh: could not connect to server: Connection refused
    Is the server running on host "localhost" (127.0.0.1) and accepting
    TCP/IP connections on port 5433?
could not connect to server: Cannot assign requested address
    Is the server running on host "localhost" (::1) and accepting
    TCP/IP connections on port 5433?
Enter fullscreen mode Exit fullscreen mode

After researching a solution, I found this solution from Frank Pachot.

docker exec -it yugabyte-yuga-1 bash -c 'ysqlsh --echo-queries 
-h $(hostname) -p 5433'
Enter fullscreen mode Exit fullscreen mode

Let's connect with a Rails app

To test this Yugabyte database, I wanted to create a Rails app.

rails new yugapp -d postgres 
Enter fullscreen mode Exit fullscreen mode

Yugabyte is a PostgreSQL-compatible database. Using the adapter for Postgres with our rails app should work. This is why I put the -d postgres option.

Now we will need to tweak a bit the database.yml file.
Indeed as we have seen above, unlike Postgres, Yugabyte is not accepting a connection on port 5432 but on 5433.
I have managed to connect to the database locally with the configuration :

default: &default
  adapter: postgresql
  encoding: unicode
  host: localhost           -
  port: 5432                 |__________ added by myself
  username: "postgres"       | 
  password: ""              -
Enter fullscreen mode Exit fullscreen mode

Run your database rake task to create, migrate and seed your database.

rails db:create db:migrate db:seed
Enter fullscreen mode Exit fullscreen mode

Remarks

The rake task is slower with Yugabyte than with Postgres.
But this is normal. Indeed, the distributed engine needs to ensure everything is synced. So the writing operations are necessarily slower.

And that's it. Now you can run your server, and it works nicely.

Conclusion

Deploying a Yugabyte database is straightforward with your Rails app.
There is more to come from Yugabyte in the future for sure. This is a really cool technology, and distributed SQL is a passionate subject.
I hope to write about the RAFT algorithm soon!

Keep in Touch

On Twitter : @yet_anotherDev

On Linkedin : Lucas Barret

Top comments (0)