-
Notifications
You must be signed in to change notification settings - Fork 2
How do I use Postgres with Rails on Vagrant?
One of the features we have packed into your student dev box is Postgres running on Vagrant's Linux Box.
Students will need this to mirror Heroku's Production environment on their local machine, since Rails uses SQLite by default.
In a Rails project follow these steps.
In your Gemfile
, include:
gem 'pg'
Take out:
gem 'sqlite3'
Then in the config/
directory, create a file called config/database.yml
.
This file will look like:
development:
adapter: postgresql
database: myappname-development
host: localhost
# 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:
adapter: postgresql
database: myappname-test
host: localhost
production:
adapter: postgresql
database: myappname-production
host: localhost
You have all the tools in place to create that database on production. But it is not yet created.
In your console, be sure to run:
heroku run rake db:migrate
If for some reason, you also need to seed some data from your seeds.rb
file make sure to run either:
heroku run rake db:migrate
heroku run rake db:seed
OR, if chaining your commands:
heroku run rake db:migrate db:seed
Right. That is true. And there is a reason: it should be harder than a common command to drop your production database. Often this is the value and the life-blood of your web-application. So they switched it up from the usual: bundle exec rake db:drop db:create ...
commands. Instead you need to run:
heroku pg:reset DATABASE
It will then prompt you to type in the name of the application. Which is the subdomain of your heroku address. For instance, for http://exampleapp.herokuapp.com
you would type: exampleapp
. Use this with extreme caution.
heroku config:set SAMPLE_API_KEY=xxxxxxxxxxxxxxxxx
If you want to be extra careful in testing your new code in a production environment, you could actually set up a "staging" server on heroku, too. There are some nuances on how you would run the heroku commands from you console that come into play when you have multiple heroku repos hooked up as remote
's. We will catalog some of that information here soon.