Getting Started

Use Supabase with Ruby on Rails

Learn how to create a Rails project and connect it to your Supabase Postgres database.


1

Create a Rails Project

Make sure your Ruby and Rails versions are up to date, then use rails new to scaffold a new Rails project. Use the -d=postgresql flag to set it up for Postgres.

Go to the Rails docs for more details.

Terminal
1
rails new blog -d=postgresql
2

Set up the Postgres connection details

Go to database.new and create a new Supabase project. Save your database password securely.

When your project is up and running, navigate to your project dashboard and click on Connect.

Look for the Session Pooler connection string and copy the string. You will need to replace the Password with your saved database password. You can reset your database password in your database settings if you do not have it.

Terminal
1
export DATABASE_URL=postgres://postgres.xxxx:password@xxxx.pooler.supabase.com:5432/postgres
3

Create and run a database migration

Rails includes Active Record as the ORM as well as database migration tooling which generates the SQL migration files for you.

Create an example Article model and generate the migration files.

Terminal
1
2
bin/rails generate model Article title:string body:textbin/rails db:migrate
4

Use the Model to interact with the database

You can use the included Rails console to interact with the database. For example, you can create new entries or list all entries in a Model's table.

Terminal
1
bin/rails console
irb
1
2
3
4
article = Article.new(title: "Hello Rails", body: "I am on Rails!")article.save # Saves the entry to the databaseArticle.all
5

Start the app

Run the development server. Go to http://127.0.0.1:3000 in a browser to see your application running.

Terminal
1
bin/rails server