PEF
PEF

Reputation: 973

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

Upvotes: 15

Views: 12079

Answers (1)

yfeldblum
yfeldblum

Reputation: 65435

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.

Upvotes: 27

Related Questions