Skip to main content
Neon Postgres Docs
current

Search documentation

Type to search this documentation.

On this pageOverview

Connect a Ruby on Rails application to Lakebase Postgres

Ruby on Rails, 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 ba...

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

Ruby on Rails, 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:

  1. Create a Neon Project

    If you do not have one already, create a Neon project.

    1. Navigate to the Projects page in the Neon Console.
    2. Click New Project.
    3. Specify your project settings and click Create Project.
  2. Create a Rails Project

    Create a Rails project using the Rails CLI, 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.

  3. 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]/

    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
  4. 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
  5. 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/ 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

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

Join our Discord Server to ask questions or see what others are doing with Neon. For paid plan support options, see Support.

Suggest an edit

Propose a replacement for this page. The site team reviews it before applying any changes.

Export
Documentation menu