Ruby on Rails
Connecting with Ruby on Rails
I am so happy that you landed on this page. I have been writing Rails since version 1.0, and I’m excited that you have chosen Crunchy Bridge, and that you have chosen Rails. Let’s get started.
Steps
- Create your Crunchy Bridge Postgres Cluster
- Configure Rails for Postgres
- Connect your Rails to Postgres
- Confirm your app is connecting to your database
- Create your database and load your schema
- Running on a server
Create your Crunchy Bridge Postgres cluster
Creating a Crunchy Bridge cluster is pretty simple. Log into your Crunchy Bridge account, and click “Create Cluster”, then follow the cluster create form.
From here, we’ll want to get a “connection string” that we can use later. It looks like this:
postgres://user:password@host:port/database?setting=value
Once your Postgres cluster has a status of “ready” (a few minutes), click on the “Connections” tab for your connection string.
We will set this value as a DATABASE_URL
value later when connecting to the
database.
Configure Rails for Postgres
Configuring Rails for Postgres is pretty simple. If you are starting from scratch, just run:
bash> rails new <rails-app-name> --database=postgresql
If you already have a Rails app and you are converting to using Postgres, then
go backup your production database, and do the following. Add the following
to your gemfile
.
gem 'pg'
Then, run bundle install
.
If you are using Rails 7, write the following to the config/database.yml
file:
<% if ENV['DATABASE_URL'].present? %>
<% database_url = URI(ENV['DATABASE_URL']) %>
default: &postgres_default
adapter: postgresql
encoding: unicode
url: <%- ENV['DATABASE_URL'] %>
production:
<<: *postgres_default
development:
<<: *postgres_default
<% elsif Rails.env.production? || Rails.env.staging? %>
raise("DATABASE_URL environment variable is required.")
<% end %>
Commentary: For database configurations, I would still use environmental
variables over Rails.application.credentials
for most situations. It feels
wrong to have to commit code to change a database URL.
In the config/database.yml
file above, we use Ruby on Rails 7 convention,
which defaults to connecting to the DATABASE_URL
value.
If you are using a version of Rails 6 or older, write the following to the
config/database.yml
file:
<% if ENV['DATABASE_URL'].present? %>
<% database_url = URI(ENV['DATABASE_URL']) %>
default: &default
adapter: postgresql
encoding: unicode
host: <%= database_url.host %>
port: <%= database_url.port %>
username: <%= database_url.user %>
password: <%= database_url.password %>
database: <%= database_url.path[1..-1] %>
production:
<<: *default
development:
<<: *default
<% end %>
For Rails 6 and older, in the database.yml
above, we use Ruby’s URI
library
to extract the values from our connection string that we’ll set in a moment.
Then, use ERB to write those values that will be loaded for production and
development.
Confirm Rails is connecting to your database
Before you go any farther, it is best to test that your Rails application is
connecting to Postgres. Below, where we have
<crunchy-bridge-connection-string>
, you will need to insert the connection
string value from the very first step above.
If you have a new application, we can test it by running the following:
bash> DATABASE_URL=<crunchy-bridge-connection-string> rails console
Once the rails console
starts, run the following:
ActiveRecord::Base.connection.execute("SELECT 1;")
If successful, you’ll see something like
#<PG::Result:0x0000000106455518 status=PGRES_TUPLES_OK ntuples=1 nfields=1 cmd_tuples=1>
.
If unsuccessful, you’ll see something like rescue in new_client
. Check the
errors that Rails emits as output, these errors will guide you to a solution.
Typically, it’s either:
- Environmental variable not set
database.yml
not configured properly
Create your database and load your schema
If you are starting from scratch, then it is quite simple, run:
bash> DATABASE_URL=<crunchy-bridge-connection-string> rails db:schema:load
If you are using an existing dataset, see our Migration Guide (or reach out to us, we are happy to help). Dump your data from your existing database, then load it into your Crunchy Bridge database.
Running on a server
On your local machine, you can run your Rails server with the following:
bash> DATABASE_URL=<crunchy-bridge-connection-string> rails server
To move from running on your local machine to running on a server, you’ll need
to set the DATABASE_URL
environmental variable on your server or app hosting
provider. For app hosting providers, just look for the setting for
“environmental variables” or “envvar”.
Once the application is running, your database will connect and use the database on the server.