Skip Navigation
Show nav
Heroku Dev Center
  • Get Started
  • Documentation
  • Changelog
  • Search
  • Get Started
    • Node.js
    • Ruby on Rails
    • Ruby
    • Python
    • Java
    • PHP
    • Go
    • Scala
    • Clojure
  • Documentation
  • Changelog
  • More
    Additional Resources
    • Home
    • Elements
    • Products
    • Pricing
    • Careers
    • Help
    • Status
    • Events
    • Podcasts
    • Compliance Center
    Heroku Blog

    Heroku Blog

    Find out what's new with Heroku on our blog.

    Visit Blog
  • Log inorSign up
View categories

Categories

  • Heroku Architecture
    • Dynos (app containers)
    • Stacks (operating system images)
    • Networking & DNS
    • Platform Policies
    • Platform Principles
  • Command Line
  • Deployment
    • Deploying with Git
    • Deploying with Docker
    • Deployment Integrations
  • Continuous Delivery
    • Continuous Integration
  • Language Support
    • Node.js
    • Ruby
      • Rails Support
      • Working with Bundler
    • Python
      • Working with Django
      • Background Jobs in Python
    • Java
      • Working with Maven
      • Java Database Operations
      • Working with the Play Framework
      • Working with Spring Boot
      • Java Advanced Topics
    • PHP
    • Go
      • Go Dependency Management
    • Scala
    • Clojure
  • Databases & Data Management
    • Heroku Postgres
      • Postgres Basics
      • Postgres Performance
      • Postgres Data Transfer & Preservation
      • Postgres Availability
      • Postgres Special Topics
    • Heroku Redis
    • Apache Kafka on Heroku
    • Other Data Stores
  • Monitoring & Metrics
    • Logging
  • App Performance
  • Add-ons
    • All Add-ons
  • Collaboration
  • Security
    • App Security
    • Identities & Authentication
    • Compliance
  • Heroku Enterprise
    • Private Spaces
      • Infrastructure Networking
    • Enterprise Accounts
    • Enterprise Teams
    • Heroku Connect (Salesforce sync)
    • Single Sign-on (SSO)
  • Patterns & Best Practices
  • Extending Heroku
    • Platform API
    • App Webhooks
    • Heroku Labs
    • Building Add-ons
      • Add-on Development Tasks
      • Add-on APIs
      • Add-on Guidelines & Requirements
    • Building CLI Plugins
    • Developing Buildpacks
    • Dev Center
  • Accounts & Billing
  • Troubleshooting & Support
  • Language Support
  • Ruby
  • Ruby Database Provisioning

Ruby Database Provisioning

English — 日本語に切り替える

Last updated 15 November 2017

Table of Contents

  • Using a different database with Rails

Due to how the buildpack release API currently functions, Heroku’s Ruby support (implemented by the Ruby buildpack) can only add an add-on to an application on the first deploy.

By default, the lowest, free Heroku Postgres plan will be added to any Ruby app that has the pg gem dependency, when deployed for the first time.

Since the default of Rails applications is to come bundled with sqlite3 instead of pg and since the add-on can only be added automatically on the first push, many developers were deploying their code, realizing that they were using the wrong database driver, and then updating their Gemfile.

If they accidentally get a successful deploy in this process without the pg gem, then the buildpack would never be able to automatically provision Heroku Postgres on subsequent deploys.

For backwards compatibility with previous stacks on Heroku, it was decided that all apps that deploy with the Rails (more specifically the railties gem) will get a database by default. This prevents the situation of when you’ve deployed before putting pg in your Gemfile, but add it in later.

Using a different database with Rails

If you are using a different database to Heroku Postgres, the default behavior of provisioning a Postgres database for Rails apps may conflict with your configuration. For detailed information about how exactly your version of Rails connects to the database please see Rails database connection behavior.

With all versions of Rails on Heroku, any information present in the DATABASE_URL environment variable will take precedence by default. This is a problem when you are not using a Postgresql database, because after Heroku adds one to your application it will “promote” it to DATABASE_URL if there is no environment variable already set. When this happens your application will try to connect to the Postgresql database by accident.

There are two strategies for dealing with this case - setting the DATABASE_URL config var, or removing it.

Setting the DATABASE_URL config var

You can set a DATABASE_URL config var on your app. This will prevent Heroku from “promoting” the Postgres database add-on:

$ heroku config:set DATABASE_URL=<scheme>://<username>:<password>@<host>

Here, you would replace the above values with the credentials and address for your database.

If you’re using pipelines or review apps, this strategy won’t work since you won’t have the ability to heroku config:set before you deploy each review app.

To remedy this you can dynamically assign the value of DATABASE_URL inside of your code. This must be done before the database is initialized. For example:

ENV["DATABASE_URL"] = "mysql://#{ ENV['MY_DATABASE_PASSWORD'] }:#{ ENV['MY_DATABASE_USERNAME'] }@#{ ENV['MY_DATABASE_HOST'] }"

You can place this in an initializer, or directly in your config/database.yml inside of an ERB code section. The important thing is that this code is run before your application attempts to connect to the database.

Deleting the DATABASE_URL config var

Alternatively if your production database credentials are in your config/database.yml you can ensure that your app does not use DATABASE_URL to connect to the database by deleting it before the connection gets initialized.

ENV.delete("DATABASE_URL")

Keep reading

  • Ruby

Feedback

Log in to submit feedback.

Using WebSockets on Heroku with Ruby Ruby Default Web Server

Information & Support

  • Getting Started
  • Documentation
  • Changelog
  • Compliance Center
  • Training & Education
  • Blog
  • Podcasts
  • Support Channels
  • Status

Language Reference

  • Node.js
  • Ruby
  • Java
  • PHP
  • Python
  • Go
  • Scala
  • Clojure

Other Resources

  • Careers
  • Elements
  • Products
  • Pricing

Subscribe to our monthly newsletter

Your email address:

  • RSS
    • Dev Center Articles
    • Dev Center Changelog
    • Heroku Blog
    • Heroku News Blog
    • Heroku Engineering Blog
  • Heroku Podcasts
  • Twitter
    • Dev Center Articles
    • Dev Center Changelog
    • Heroku
    • Heroku Status
  • Facebook
  • Instagram
  • Github
  • LinkedIn
  • YouTube
Heroku is acompany

 © Salesforce.com

  • heroku.com
  • Terms of Service
  • Privacy
  • Cookies