1

First off, I'm a total Rails/PostgreSQL noob.

I'm trying to deploy a rails app to Heroku. I've gotten it up and running, but every time I deploy, Heroku ignores my database.yml information and generates a new database. Here is my database.yml

# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
#
default: &default
 adapter: sqlite3
 pool: 5
 timeout: 5000

development:
 # <<: *default
 #  database: db/development.sqlite3
 adapter: postgresql
 pool: 5
 host: wwydh.cqqq2sesxkkq.us-east-1.rds.amazonaws.com
 timeout: 5000
 username: wwydh_a_team
 password: really cool password
 database: wwydh
 host: wwydh.cqqq2sesxkkq.us-east-1.rds.amazonaws.com
 port: 5432


# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
 <<: *default
 database: db/test.sqlite3

What am I doing wrong?

Nick Winner
  • 261
  • 2
  • 11

2 Answers2

2
heroku config:add DATABASE_URL=postgres://{user}:{password}@{hostname}:{port}/{database-name}

Then re-deploy your app. It will read your DATABASE_URL and generate the database.yml from that.

EDIT
According to this SO question The database needs to be detached first, then update the variable DATABASE_URL.

heroku addons:attach heroku-postgresql -a <app_name> --as HEROKU_DATABASE
heroku addons:detach DATABASE -a <app_name>
heroku config:add DATABASE_URL=.....
Community
  • 1
  • 1
Tony Vincent
  • 13,354
  • 7
  • 49
  • 68
  • Thank you for your answer! When I ran the first command (with and without braces) I get DATABASE_URL:postgress://wwydh_a_team:password@wwydh.cqqq2sesxkkq.us-east-1.rds.amazonaws.com:5432/wwydh is invalid. Must be in the format FOO=bar. – Nick Winner Sep 12 '16 at 04:44
  • Update, I changed DATABASE_URL:postgres to DATABASE_URL=postgres and the command ran, but I got "Cannot overwrite attachment values DATABASE_URL." – Nick Winner Sep 12 '16 at 04:47
  • It looks like database needs to be detached first. Plz see my edit – Tony Vincent Sep 12 '16 at 05:14
  • Thank you for pointing me in the right direction, I posted an answer with what I did to fix it. :) – Nick Winner Sep 12 '16 at 15:08
0

Finally solved it! This is what I did, maybe it'll help somebody.

Heroku defaults your app to 'production' status, which means it will use the settings defined in your database.yml file under 'production'. Since I didn't have any of those settings, it was creating a database to use. I deleted my Heroku PostgreSQL database (the one that was created for me), and manually switched my RAILS_ENV variable (located under app settings in Heroku) to 'development' and voila! My app connected to my external AWS database no problem.

Side note, make sure you define a new security group in AWS to allow incoming connections from any Inbound Source, IE 0.0.0.0/0. Otherwise, the database will deny Heroku access. I went to the VPC dashboard in AWS, clicked security groups, and edited my default group to this, then went and made sure that my database instance was using that security group.

Nick Winner
  • 261
  • 2
  • 11