Install the Ruby PostgreSQL driver (pg) using the following command:

$ gem install pg -- --with-pg-config=<yugabyte-install-dir>/postgres/bin/pg_config

For more information on the driver, see the pg driver documentation.

Create the application

Create a file yb-sql-helloworld.rb and add the following content to it.

#!/usr/bin/env ruby

require 'pg'

  # Output a table of current connections to the DB
  conn = PG.connect(host: '', port: '5433', dbname: 'yugabyte', user: 'yugabyte', password: 'yugabyte')

  # Create table
  conn.exec ("CREATE TABLE employee (id int PRIMARY KEY, \
                                     name varchar, age int, \
                                     language varchar)");

  puts "Created table employee\n";

  # Insert a row
  conn.exec ("INSERT INTO employee (id, name, age, language) \
                            VALUES (1, 'John', 35, 'Ruby')");
  puts "Inserted data (1, 'John', 35, 'Ruby')\n";

  # Query the row
  rs = conn.exec ("SELECT name, age, language FROM employee WHERE id = 1");
  rs.each do |row|
    puts "Query returned: %s %s %s" % [ row['name'], row['age'], row['language'] ]

rescue PG::Error => e
  puts e.message
  rs.clear if rs
  conn.close if conn

Run the application

To use the application, run the following command:

$ ./yb-sql-helloworld.rb

You should see the following output.

Created table employee
Inserted data (1, 'John', 35, 'Ruby')
Query returned: John 35 Ruby