I'd like to copy the Postgres database from a production environment to a test environment, but it doesn't seem that the Heroku CLI is available on the Heroku app server (heroku run bash, then try running heroku).
Ideally I could do this using Heroku Scheduler, but for now I'm running this command locally:
heroku pg:copy [myapp]-prod::postgresql-[stack]-[id] postgresql-[stack]-[id] --app [myapp]-test
Any thoughts or suggestions?
Related
I have a db locally in my Rails app. The app is alreqady linked to Heroku through git. I want to totally replace my development db with the one in production on Heroku . How do it ? Are there a set of commands I can use locally to do the job for me ?
You can easily do it with help of heroku pg:backups:restore command. see the detail document here. If you do not have any s2 url for dump install ngrok and put your file in public folder and provide that ngrok url of dump. This will help.
There's a Heroku command for that:
heroku pg:pull HEROKU_POSTGRESQL_MAGENTA mylocaldb --app sushi,
where you replace each of those arguments with your app's information.
(https://devcenter.heroku.com/articles/heroku-postgresql)
You could write a short rake task to fully replace your local db with Heroku using that command:
lib/tasks/db.rake
desc 'replaces local database with Heroku production database'
task replace_local_with_production: :environment do
Rake::Task['db:drop'].invoke
system heroku pg:pull HEROKU_POSTGRESQL_MAGENTA mylocaldb --app sushi
Rake::Task['db:migrate'].invoke
end
so i've successfully installed Harrys Prelauncher on Heroku (https://github.com/harrystech/prelaunchr)
and to export my collected emails into a csv i need to run this command (bundle exec rake prelaunchr:create_winner_csvs)
is there any way to run that command through pgadmin or some other program?
or is the only way for me to download my heroku database and run the command locally? also how and what would i need to do this?
i'm pretty new to rails and postgresql and would really appreciate if someone could help me out!
Because the rake task creates files locally you can't just run it on heroku via heroku run rake. You can however set up your local database.yml to connect to your heroku postgresql instance and run the rake task locally.
Run heroku pg:credentials to get the required database values.
Fill in the production environment of config/database.yml with the values you obtained from step 1 (for the value of 'database' in the yml file, use dbnmae from step 1)
Test your connection with RAILS_ENV=production rails db. This should drop you into a psql console.
Run the rake task. RAILS_ENV=production bundle exec rake prelaunchr:create_winner_csvs
The files will save locally in lib/assets as indicated by the documentation.
From within the directory of the project you can use
heroku run rake prelaunchr:create_winner_csvs
You should probably create a UI form in to your application.
On click on export CSV, it should run background job on heroku (Using delayed jobs).
heroku run rake prelaunchr:create_winner_csvs
Use send_data ruby method. To send your generated and dumped data file on to your browser.
Download the file on to your local system from running heroku instance.
Hope this will resolve your problem.
Cheers!!!
I ran into this problem recently while developing the Prelaunchr campaign for a client. Assuming you have a local version of your app and are using Postgres Copper in Heroku, you can "pull" your Heroku database down to your local machine, set that as your development database in database.yml, and run the rake task from your local app, which should now have the same database as your heroku version. Here is the command to pull the db (subbing out name_for_database & heroku_app_name with your own):
heroku pg:pull HEROKU_POSTGRESQL_COPPER_URL name_for_database --app heroku_app_name
Make sure to restart your local server to see the new database info populated.
I often do heroku db:pull and it works great.
I have created a staging app, to review code changes on Heroku before deploying to the production app.
I wonder though how I push db data specifically to the staging app?
Will the follwing work? heroku db:push -a my-staging-app-name
heroku db:push -a my-staging-app-name
will only push your local data to heroku. but it has some problems
I suggest you to push production db directly to staging server
For that you need to get the URL from production and run following command.
heroku pgbackups:restore DATABASE 'db_dump_url' -a my-staging-app-name
I'm running two apps on Heroku, one being myapp-production and the other one being myapp-staging.
For some reason however, the staging app uses the production environment configuration rather than the staging configuration, which I have defined in staging.rb.
The way I set up the staging app was by running
heroku create --stack cedar --remote staging
and afterwards
heroku config:add RACK_ENV=staging --app myapp-staging
Yet myapp-staging is still using the production configuration. Any ideas why and how to address that?
It's important to add RAILS_ENV=staging too
heroku config:add RAILS_ENV=staging --app myapp-staging
Did you restart your dynos (heroku restart) after making this change to your configuration?
I've been working on a Heroku app for several months. We've recently set up a staging server and occasionally sync the staging db with the production db. The three main commands in use are:
$ heroku pgbackups:capture --app myapp
$ heroku pg:reset DATABASE --app myapp-staging --confirm myapp-staging"
$ heroku pgbackups:restore DATABASE `heroku pgbackups:url --app myapp` --app myapp-staging
The problem is that after running the third command, I need to run heroku run rake db:migrate --app myapp-staging. We have a few dozen migrations now, including some that refer to Ruby classes that we've deleted or renamed.
This causes the migrations to fail to fully run. What's the solution here? Should I delete the old migrations that fail and commit these changes to the git repo?
Re-running this script fixed the error, so it seems like the schema should just copy over. For anyone seeing a failing migration like I did, the pgbackups:restore command probably failed for you, so re-run that.
You can also checkout the transfer command now as part of pgbackups .. see this post
How do I transfer production database to staging on Heroku using pgbackups? Getting error