Deploy Rails with Yugabyte
Tags: database, rails
Published: 2023-07-03 18:12:02

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.

```yaml
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" 
```

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 
```shell
docker exec -it yugabyte-yuga-1 /home/yugabyte/bin/ysqlsh --echo-queries
```

But it turns out that I get this error : 
```shell
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?
```

After researching a solution, I found [this solution](https://forum.yugabyte.com/t/ysqlsh-could-not-connect-to-server-connection-refused/848/8) from Frank Pachot.

```shell
docker exec -it yugabyte-yuga-1 bash -c 'ysqlsh --echo-queries 
-h $(hostname) -p 5433'
```

## Let's connect with a Rails app

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

```shell
rails new yugapp -d postgres 
```

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 : 

```yaml
default: &default
  adapter: postgresql
  encoding: unicode
  host: localhost           -
  port: 5432                 |__________ added by myself
  username: "postgres"       | 
  password: ""              -
```

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

```shell
rails db:create db:migrate db:seed
```


### 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] (https://twitter.com/yet_anotherDev)

On Linkedin : [Lucas Barret](https://www.linkedin.com/in/lucas-barret-67194b164/)