This application connects to your YugabyteDB instance via the
Ruby Pg driver for PostgreSQL and performs basic SQL operations. The instructions below are provided for YugabyteDB Aeon deployments.
If you use a different type of deployment, then update the sample-app.rb file with proper connection parameters.
- Ruby 3.1 or later.
- OpenSSL 1.1.1 or later (used by libpq and Ruby Pg to establish SSL connections).
- libpq - the official PostgreSQL driver for C (required by Ruby Pg).
- Ruby Pg - the official Ruby driver for PostgreSQL.
- Command line tool or your favourite IDE, such as Visual Studio Code.
- Start YugabyteDB Aeon instance. You can use the free tier at no cost.
- Add an IP address of your machine/laptop to the IP allow list
Clone the repository and change dirs into it:
git clone https://github.com/YugabyteDB-Samples/yugabyte-simple-ruby-app.git && cd yugabyte-simple-ruby-appLocate and define the following connection parameters in the sample-app.rb file:
host- the hostname of your instance.user- the username for your database.password- the password for your database.sslmode- an SSL mode. Make sure it's set toverify-fullfor YugabyteDB Aeon.sslrootcert- a full path to your CA root cert (for example,/Users/dmagda/certificates/root.crt).
Note, you can easily find all the settings on the YugabyteDB Aeon dashboard:
-
Install the libpq driver (required by Ruby Pg):
- Homebrew users can install using the
brew install libpqcommand (find details here). - Others can download the PostgreSQL binaries and source from the PostgreSQL Downloads page.
- Homebrew users can install using the
-
Replace
{path-to-libpq}with the path to the libpq installation and install Ruby Pg:gem install pg -- --with-pg-include={path-to-libpq}/libpq/include --with-pg-lib={path-to-libpq}/libpq/libFind alternative installation instructions here.
-
Make the application file executable:
chmod +x sample-app.rb
-
Run the application:
./sample-app.rb
Upon successful execution, you will see output similar to the following:
>>>> Connecting to YugabyteDB!
>>>> Successfully connected to YugabyteDB!
>>>> Successfully created table DemoAccount.
>>>> Selecting accounts:
name=Jessica, age=28, country=USA, balance=10000
name=John, age=28, country=Canada, balance=9000
>>>> Transferred 800 between accounts.
>>>> Selecting accounts:
name=Jessica, age=28, country=USA, balance=9200
name=John, age=28, country=Canada, balance=9800Congrats! You've successfully executed a simple Ruby app that works with YugabyteDB.
Now, explore the source code of sample-app.rb:
connectfunction - establishes a connection with your cloud instance via the Ruby Pg driver.create_databasefunction - creates a table and populates it with sample data.select_accountsfunction - queries the data with SQLSELECTstatements.transfer_money_between_accountsfunction - updates records consistently with distributed transactions.
Having issues running this application or want to learn more from Yugabyte experts?
Join our Slack channel,
or raise a question on StackOverflow and tag the question with yugabytedb!
