I have an existing Ruby on Rails app that already has data loaded into it.
I used the default SQLite database setup, so that is where all my data is located, but I need all my data to go into my Postgres database on heroku.
How do I do that?
10 Minutes Move from Local SQLite to a Heroku Postgres
-- updates your local dev to postgres along the way --
This is assuming you have a development database in sqlite and you want to move the structure and data to heroku. You will be first changing your local environment to postgres, then moving it all up.
Why change? You should always have your development environment mirror your production environment. Using Postgres is the default on heroku.
You will need to install and configure Postgres locally first with a user that has your username
Software needed: postgresql, pgloader, heroku-cli
Steps
Move from SQLite to Postgres on your dev environment
- install heroku / pgloader / postgres, and make sure postgresql is running on your system
- backup sqlite - copy development.sql to development_old.sql
- add
gem 'pg'
to main section of your Gemfile - bundle install
- update config/database.yml (see sample below)
- rake db:setup
- cd [application root]
- load postgres db with data -
pgloader ./db/development.sqlite3 postgresql:///[name of postgres dev db]
- remove
gem 'sqlite3'
- bundle install
- start server -
rails server
- test by visiting app at localhost:3000
Setup new app on heroku
Follow these instructions from heroku
Move data to heroku
- find heroku db info -
heroku pg:info
- erase and reset remote db -
heroku pg:reset DATABASE_URL --app [name of app]
- push local data to heroku -
heroku pg:push [name of postgres dev db] DATABASE_URL --app [name of app]
NOTE: if that database has greater than 10k rows, you will also need to upgrade to a hobby-basic tier on heroku
Upgrading Heroku to Hobby Tier Basic
- create new tier - `heroku addons:create heroku-postgresql:hobby-basic --app [name of app]
- get the new database url -
heroku pg:info
- turn on maintenance -
heroku maintenance:on --app [name of app]
- copy data -
heroku pg:copy DATABASE_URL [HEROKU_POSTGRESQL_COLOR_URL] --app [name of app]
- promote new db -
heroku pg:promote [HEROKU_POSTGRESQL_COLOR_URL] --app [name of app]
- turn off maintenance
- test by visiting heroku app
In case you run into issues or edge cases, here are some resources to help.
Resources:
- https://pgloader.io
- postgres install docs
- heroku new rails install
- heroku cli info
- using the heroku cli
database_sample.yml
default: &default
adapter: postgresql
encoding: unicode
host: localhost
port: 5432
# For details on connection pooling, see Rails configuration guide
# http://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
development:
<<: *default
database: [name of app]_dev
test:
<<: *default
database: [name of app]_test
staging:
<<: *default
database: [name of app]
production:
<<: *default
database: [name of app]
Hey dude you have all you need inside the link below
How to change from SQLite to PostgreSQL and deploy on heroku
let me know if you any more doubts regards
来源:https://stackoverflow.com/questions/51068610/how-do-i-move-my-existing-rails-app-onto-heroku-sqlite-to-postgres