Skip to content

Using XTDB from Ruby

In Ruby, you can talk to a running XTDB node using the Sequel gem or the native pg gem, taking advantage of XTDB’s PostgreSQL wire-compatibility.

To install the Sequel gem, add it to your Gemfile:

gem 'sequel'
gem 'pg' # PostgreSQL adapter

Or install directly:

Terminal window
gem install sequel pg

Once you’ve started your XTDB node, you can use the following code to connect to it:

require 'sequel'
DB = Sequel.connect("xtdb://xtdb:5432/xtdb")
DB << "INSERT INTO ruby_users RECORDS {_id: 'alice', name: 'Alice'}, {_id: 'bob', name: 'Bob'}"
puts "Users:"
DB["SELECT _id, name FROM ruby_users"].each do |row|
puts " * #{row[:_id]}: #{row[:name]}"
end
puts "\n✓ XTDB connection successful"
# Output:
#
# Users:
# * alice: Alice
# * bob: Bob
#
# ✓ XTDB connection successful

For more examples and tests, see the XTDB driver-examples repository, which contains comprehensive test suites demonstrating various features and use cases.