Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

2.5 KB
Raw Blame
---
title: 'Use Supabase with Ruby on Rails'
subtitle: 'Learn how to create a Rails project and connect it to your Supabase Postgres database.'
breadcrumb: 'Framework Quickstarts'
---

<AiPrompt id="ruby-on-rails" />

<$Partial path="quickstart_create_project.mdx" />

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

<Admonition type="note">

This guide uses Rails' own Active Record models and migrations, not the shared `instruments` sample table used by other quickstarts.

</Admonition>

## 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](https://guides.rubyonrails.org/getting_started.html) for more details.

```bash
rails new blog -d=postgresql
cd blog
```

## 3. Set up AI tooling (optional)

<$Partial path="quickstart_ai_tooling.mdx" />

## 4. Set up the Postgres connection details

<$Partial path="quickstart_connection_string.mdx" />

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.

```bash
export DATABASE_URL=postgres://postgres.[PROJECT-REF]:[YOUR-PASSWORD]@[POOLER-HOST]:5432/postgres?sslmode=require
```

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

```bash
bin/rails generate model Article title:string body:text
bin/rails db:migrate
```

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

```bash
bin/rails console
```

```rb name=irb
article = Article.new(title: "Hello Rails", body: "I am on Rails!")
article.save # Saves the entry to the database

Article.all
```

## 7. Start the app

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

```bash
bin/rails server
```

<$Partial path="quickstart_going_to_production.mdx" />

## Next steps

- Set up [Auth](/docs/guides/auth) for your app
- [Insert more data](/docs/guides/database/import-data) into your database
- Upload and serve static files using [Storage](/docs/guides/storage)