Skip to content
Getting Started

Use Supabase with Ruby on Rails

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

AI Prompt
Help me add Supabase to my Ruby on Rails project. Create a Supabase project at database.new. Then: 1. Run `rails new blog -d=postgresql` to scaffold a new Rails project. 2. Set `DATABASE_URL` to the Supabase Session Pooler connection string in `.env`. 3. Generate an Article model with `bin/rails generate model Article title:string body:text` and run `bin/rails db:migrate`. 4. Use `bin/rails console` to create and query articles. 5. Run `bin/rails server` and open http://127.0.0.1:3000. REFERENCE https://supabase.com/docs/guides/getting-started/quickstarts/ruby-on-rails.md

1. Create a Supabase project#

To start, you need a Supabase project.

Create a new Supabase project from the Dashboard of any organization you belong to.

Save your database password securely. You need it for the connection string.

2. Create a Rails project#

With your Ruby and Rails versions up to date, run rails new on your terminal to scaffold a new project.

Use the -d=postgresql flag to set it up for Postgres.

Check the Rails docs for more details.

1
rails new blog -d=postgresql
2
cd blog

3. Install Supabase's Agent Skills (optional)#

Supabase's Agent Skills is a curated set of instructions that give your AI agent procedural knowledge about working with Supabase.

Install them so your AI coding agent can produce more accurate, reliable code using current Supabase patterns, such as authentication, server-side rendering, and database migrations, rather than relying solely on training data.

To install, run the following command in the root of your project:

1
npx skills add supabase/agent-skills

4. Install MCP server (optional)#

The Supabase MCP server connects AI assistants to Supabase, allowing you to interact with your projects on your behalf. Find out more on how to add it to your client in the MCP docs.

5. Set up the Postgres connection details#

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, and percent-encode any reserved characters it contains, such as &, #, ?, or a space. You can reset your database password in your Database Settings if you do not have it.

Set the connection string as an environment variable. Rails reads DATABASE_URL from the environment and connects with it, so you don't need to edit config/database.yml. The export applies to the current shell session, so run it in the same shell as the Rails commands in the following steps. sslmode=require stops the driver from falling back to sending your data in plaintext.

1
export DATABASE_URL=postgres://postgres.[PROJECT-REF]:[YOUR-PASSWORD]@aws-[REGION].pooler.supabase.com:5432/postgres?sslmode=require

6. 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.

1
bin/rails generate model Article title:string body:text
2
bin/rails db:migrate

7. 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.

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

8. Start the app#

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

1
bin/rails server

Next steps#