Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Database connection on Heroku

Wow I've been stuck on this one for days. I'm having trouble connecting to database.yml on Heroku. I'm on Cedar and ruby 1.9.2. My dev and test dbs are sqlite3 and the prod db is postgreSQL to cope with Cedar rules. Here is the code in my ruby script:

Rails.env.production? ? (env = "production") : (env = "development")
dbconfig = YAML::load(File.open('config/database.yml'))[env]
ActiveRecord::Base.establish_connection(dbconfig)

All goes well in local but when I push to Heroku, I get:

ArgumentError: syntax error on line 17, col 0: `adapter = uri.scheme'
from /usr/local/lib/ruby/1.9.1/syck.rb:135:in `load'

It looks like Heroku doesn't like my database.yml. Here's an overview:

development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

test:
  adapter: sqlite3
  database: db/test.sqlite3
  pool: 5
  timeout: 5000

production:
  adapter: postgresql
  encoding: unicode
  database: foo
  port: 5432
  host: foobar.amazonaws.com
  username: foo
  password: bar
like image 640
PEF Avatar asked Sep 08 '11 17:09

PEF


People also ask

Does Heroku support database?

Managed PostgreSQL from HerokuHeroku Postgres delivers the world's most advanced open source database as a trusted, secure, and scalable service that is optimized for developers.

How do I connect my PostgreSQL database to Heroku?

If you want to connect to Heroku Postgres, create a data source connection that correspond to the data source vendor. In this case, you plan to work with PostgreSQL, so you need to create a connection to PosgreSQL. DataGrip already include the necessary JDBC driver.


1 Answers

First, Heroku overwrites your config/database.yml with its own Heroku-specific version. That's how Heroku automatically connects your application to its own postgresql databases. To tell Heroku about your own posgresql database, you should set up the correct config variables and you might as well omit the production database from your the config/database.yml in your repository because Heroku will ignore it anyway.

Second, the config/database.yml file is an ERB template for a YAML file. You must first run the file contents through Evaluated Ruby (ERB) before running the the output through YAML.

like image 146
yfeldblum Avatar answered Oct 11 '22 02:10

yfeldblum