Nick Winner
Nick Winner

Reputation: 261

Deploy rails to Heroku using outside postgres database

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?

Upvotes: 1

Views: 380

Answers (2)

Nick Winner
Nick Winner

Reputation: 261

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.

Upvotes: 0

Tony Vincent
Tony Vincent

Reputation: 14362

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=.....

Upvotes: 2

Related Questions