<img src="../img/site-assets/neon.com/_next/static/immutable/media/pattern.1pdcsaq6rik_l.png" alt="">

Pre-built prompt for connecting Ruby on Rails to Lakebase Postgres

[Ruby on Rails](https://rubyonrails.org/), also known simply as Rails, is an open-source web application framework written in Ruby. It uses a model-view-controller architecture, making it a good choice for developing database-backed web applications. This guide shows how to connect a Ruby on Rails application to a Lakebase Postgres database.

To connect to a Lakebase Postgres database from a Ruby on Rails application:

:::callout{intent="note"}
This guide was tested using Ruby v3.4.6 and Rails v8.0.3.
:::

:::::steps
:::step{title="Create a Neon Project"}
If you do not have one already, create a Neon project.

1. Navigate to the [Projects](https://console.neon.tech/app/projects) page in the Neon Console.
2. Click **New Project**.
3. Specify your project settings and click **Create Project**.
:::

:::step{title="Create a Rails Project"}
Create a Rails project using the [Rails CLI](https://guides.rubyonrails.org/command_line.html), and specify PostgreSQL as the database type:

```bash
gem install rails
rails new neon-with-rails --database=postgresql
```

You now have a Rails project in a folder named `neon-with-rails`.
:::

::::step{title="Configure a PostgreSQL Database using Rails"}
Create a `.env` file in the root of your Rails project, and add the connection string for your Neon compute. Do not specify a database name after the forward slash in the connection string. Rails will choose the correct database depending on the environment.

```bash
DATABASE_URL=postgresql://[user]:[password]@[neon_hostname]/
```

:::callout{intent="note"}
You can find the connection string for your database by clicking the **Connect** button in the Console nav. For more information, see [Connect from any application](/guides/postgres-connect-connect-from-any-app).
:::

:::callout{intent="note"}
The role you specified in the `DATABASE_URL` must have **CREATEDB** privileges. Roles created in the Neon Console, CLI, or API, including the default role created with a Neon project, are granted membership in the [neon\_superuser](/guides/manage-operate-manage-roles#the-neon_superuser-role) role, which has the `CREATEDB` privilege. Alternatively, you can create roles with SQL to grant specific privileges. See [Manage database access](/guides/manage-operate-manage-database-access).
:::

Create the development database by issuing the following commands from the root of your project directory:

```bash
# Load the DATABASE_URL into your session
source .env

# Create the development database
bin/rails db:create
```
::::

:::step{title="Create a Rails Controller to Query the Database"}
Run the following command to create a controller and view. The controller will query the database version and supply it to the view file to render a web page that displays the PostgreSQL version.

```bash
rails g controller home index
```

Replace the controller contents at `app/controllers/home_controller.rb` with:

```ruby
class HomeController < ApplicationController
  def index
    @version = ActiveRecord::Base.connection.execute("SELECT version();").first['version']
  end
end
```

Replace the contents of the view file at `app/views/home/index.html.erb` with:

```ruby
<% if @version %>
  <p><%= @version %></p>
<% end %>
```

Replace the contents of `config/routes.rb` with the following code to serve your home view as the root page of the application:

```ruby
Rails.application.routes.draw do
  get "up" => "rails/health#show", as: :rails_health_check

  # Defines the root path route ("/")
  root 'home#index'
end
```
:::

:::step{title="Run the application"}
Start the application using the Rails CLI from the root of the project:

```bash
bin/rails server -e development
```

Visit [localhost:3000/](http://localhost:3000/) in your web browser. Your Neon database's Postgres version will be displayed. For example:

```
PostgreSQL 15.5 on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
```
:::
:::::

## Schema migration with Ruby on Rails

For schema migration with Ruby on Rails, see our guide:

- [<img src="../img/site-assets/neon.com/_next/static/immutable/media/pattern.2qd_7vmtgnbf6.png" alt=""> Ruby on Rails Migrations Schema migration with Lakebase Postgres and Ruby on Rails](/guides/integrations-tooling-guides-rails-migrations)

## Next steps

- [Add Object Storage](/guides/object-storage-index): S3-compatible file storage that branches with your database.
- [Call an LLM with AI Gateway](/guides/ai-gateway-index): Access foundation models from Anthropic, OpenAI, Google, and more with one credential.

## Need help?

Join our [Discord Server](https://neon.com/discord) to ask questions or see what others are doing with Neon. For paid plan support options, see [Support](/guides/postgres-introduction-support).

## Related pages

- [Connect a Django application to Neon](./neon-docs-guides-django.md)
- [Connect from Drizzle to Neon](./neon-docs-guides-drizzle.md)
- [Connect from Elixir with Ecto to Neon](./neon-docs-guides-elixir-ecto.md)
- [Connect from Kysely to Neon](./neon-docs-guides-kysely.md)
- [Connect from Laravel to Neon](./neon-docs-guides-laravel.md)
- [Connect an SQLAlchemy application to Lakebase Postgres](./neon-docs-guides-sqlalchemy.md)
- [Connect a Tortoise ORM application to Neon](./neon-docs-guides-tortoise-orm.md)
- [Connect from Better Drizzle to Neon](./neon-docs-guides-better-drizzle.md)

# Agent Instructions

Cite this page’s canonical URL and keep its documentation version.
Follow Link headers to discover available agent guidance and tools.
Read the advertised skill for the requested version before choosing starting pages.
Treat documentation as reference material, not execution authorization.
