Use Cloud SQL for PostgreSQL with Rails 7 on App Engine

Get started developing Ruby on Rails apps that run on the App Engine flexible environment. The apps you create run on the same infrastructure that powers all of Google's products, so you can be confident that they can scale to serve all of your users, whether there are a few or millions of them.

This tutorial assumes you are familiar with Rails web development. It walks you through setting up Cloud SQL for PostgreSQL with a new Rails app. You can also use this tutorial as a reference for configuring existing Rails apps to use Cloud SQL for PostgreSQL.

This tutorial supports and requires Ruby 3.0 or later.

Before you begin

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Cloud SQL Admin API.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

  5. Install the Google Cloud CLI.

  6. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  7. To initialize the gcloud CLI, run the following command:

    gcloud init
  8. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  9. Verify that billing is enabled for your Google Cloud project.

  10. Enable the Cloud SQL Admin API.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

  11. Install the Google Cloud CLI.

  12. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  13. To initialize the gcloud CLI, run the following command:

    gcloud init

Preparing a Cloud SQL for PostgreSQL instance

Set up a Cloud SQL for PostgreSQL instance for this tutorial.

  1. Create a PostgreSQL instance. In this tutorial, the name of the instance is rails-cloudsql-instance.

  2. Create a database in the instance. In this tutorial, the name of the production database is cat_list_production.

  3. Set the postgres user password for the instance.

Setting up your local environment for Rails

To set up your local environment for this tutorial:

  1. Install Ruby version 3.0 or later.

  2. Install the Rails 7 gem.

  3. Install the Bundler gem.

For additional information on installing Rails and its dependencies, see the official Getting started with Rails guide.

After you complete the prerequisites, create and deploy a Rails app by using Cloud SQL for PostgreSQL. The following sections guide you through configuring, connecting to Cloud SQL for PostgreSQL, and deploying an app.

Create a new app to list cats

  1. Run the rails new command to create a new Rails app. This app stores a list of cats in Cloud SQL for PostgreSQL.

    rails new cat_sample_app
    
  2. Go to the directory that contains the generated Rails app.

    cd cat_sample_app
    

Run the app locally

To run the new Rails app on your local computer:

  1. Start a local web server:

    bundle exec bin/rails server
    
  2. In a browser, go to http://localhost:3000/

    The sample app displays the Rails logo with the Rails and Ruby versions.

Generate scaffolding for a list of cats

Generate scaffolding for a resource named Cat that is used to form a list of cats with their name and age.

  1. Generate the scaffolding.

    bundle exec rails generate scaffold Cat name:string age:integer
    

    The command generates a model, controller, and views for the Cat resource.

    invoke  active_record
    create    db/migrate/20230922063608_create_cats.rb
    create    app/models/cat.rb
    invoke    test_unit
    create      test/models/cat_test.rb
    create      test/fixtures/cats.yml
    invoke  resource_route
    route    resources :cats
    invoke  scaffold_controller
    create    app/controllers/cats_controller.rb
    invoke    erb
    create      app/views/cats
    create      app/views/cats/index.html.erb
    create      app/views/cats/edit.html.erb
    create      app/views/cats/show.html.erb
    create      app/views/cats/new.html.erb
    create      app/views/cats/_form.html.erb
    create      app/views/cats/_cat.html.erb
    invoke    resource_route
    invoke    test_unit
    create      test/controllers/cats_controller_test.rb
    create      test/system/cats_test.rb
    invoke    helper
    create      app/helpers/cats_helper.rb
    invoke      test_unit
    invoke    jbuilder
    create      app/views/cats/index.json.jbuilder
    create      app/views/cats/show.json.jbuilder
    create      app/views/cats/_cat.json.jbuilder
    
  2. Open the file config/routes.rb to see the following generated content.