Skip to main content

Ruby

Relyt supports the PostgreSQL connection protocol, which means any tools or software that can connect to PostgreSQL can also be used for Relyt connections.

This guide will walk you through the process of connecting to Relyt using Ruby.

info

The OS used in the following example is MacOS. If you use other types of OS, change the code accordingly.

1. Prepare the environment

Install Ruby and the PostgreSQL Ruby library in your environment.

tip

Skip this step if your environment is ready.

brew install ruby
gem install pg

2. Create a Ruby program

  1. Create a .rb file:

    vim relyt_demo.rb
  2. Write the program code in the file:

    require 'pg'

    begin
    conn = PG.connect :host => '<endpoint_domain>', :port => <endpoint_port>, :dbname => '<database>', :user => '<dw_user>', :password => '<dw_user_password>'

    conn.exec "DROP TABLE IF EXISTS items;"
    conn.exec "CREATE TABLE IF NOT EXISTS items (id INTEGER, name VARCHAR);"
    conn.exec "INSERT INTO items (id, name) VALUES (1, 'item1'), (2, 'item2');"

    result = conn.exec "SELECT * FROM items;"
    result.each do |row|
    puts row
    end

    rescue PG::Error => e
    puts e.message

    ensure
    conn.close if conn
    end

    The following table describes the related parameters. Replace them with the actual values when in practice.

    ParameterDescription
    endpoint_domainThe public endpoint for connecting to the Relyt DW service unit.
    endpoint_portThe port for connecting to the Relyt DW service unit. The default port is 5432.
    dw_userThe name of the DW user that connects to the Relyt DW service unit.
    dw_user_passwordThe password of the DW user.
    databaseThe name of the Relyt database to connect.

    The SQL commands provided in the example above perform the following operations in sequence:

    • Drop a table

    • Create a table

    • Insert data to a table

    • Query data

    They are for reference only. You can replace them with any SQL commands you want to run.

3. Run the program

Run the program:

ruby relyt_demo.rb