49 Days of Ruby: Day 31 - Working with Databases
Tags: ruby
Published: 2021-04-27 03:04:46

---
title: 49 Days of Ruby: Day 31 - Working with Databases
published: true
description: Join us on a 49 day learning journey through Ruby!
series: 49 Days of Ruby
tags: #ruby
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/9m73qaetuse3nm3y3it8.png
---

**Welcome to day 31 of the 49 Days of Ruby! 🎉**

Databases are a really big subject. Today, we are going to take a few steps into the ocean of databases. We'll spend a moment explaining what they are, and offering a tip or two on working with them in Ruby.

## What is a Database?

At its most fundamental level, a database is a piece of software that stores information. That information is stored in *tables*, and each *table* has *rows* and *columns* of data.

![SQL Query](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gd0dmiplo5d0hktup6up.png)
<figcaption>Database Example from <a href="https://en.wikipedia.org/wiki/Database#/media/File:DVD_Rental_Query.png">Wikipedia</a></figcaption>

Databases are incredibly important if you want your application to keep information in between uses. Let's say you build a website with member profiles. Should a member re-create their profile each time they log in? Probably not. You would store their data in a member database.

## Databases in Ruby

There are different types of databases with different languages used to communicate to them. The most common you will encounter with Ruby applications are *SQL* databases.

SQL is a language for writing database queries. It is how you ask the database for information, add info to the database, and generally move around your data.

Within SQL-type databases, there are lots of different options. In this example, we'll show how to get started with PostgreSQL, a prevalent database type.

The [ruby-pg](https://github.com/ged/ruby-pg) gem provides an interface between your Ruby code and your PostgreSQL database.

You would require the gem like any other gem in your code:

```ruby
require 'pg'
```

Then, you establish a connection your PostgreSQL database providing the database name and the database username:

```ruby
connection = PG.connect :dbname => 'my_database', :user => 'database_user'
```

Now you have an instantiated connection to your database, you can start writing SQL queries to it!

W3Schools has an [intro to SQL](https://www.w3schools.com/sql/sql_intro.asp) that may be helpful to read.

Let's say you want to retrieve all your coffee records:

```ruby
results = connectio.exec "SELECT * FROM Coffees"
```

The result will be all the coffee records in the `results` variable. You can then output it using an iterator:

```ruby
results.each do |row|
  puts "#{row['coffee_name]}: #{coffee['caffeine_strength']}"
end
```

I made up those column names of `coffee_name` and `caffeine_strength` for the example. Your database will be whatever you specify.

There is **so** much more to learn about databases. Share what you discover today with the community!

**Come back tomorrow for the next installment of 49 Days of Ruby! You can join the conversation on Twitter with the hashtag [#49daysofruby](https://twitter.com/hashtag/49daysofruby).**