Created
July 6, 2016 08:39
-
-
Save willwright82/930180ea763ab5558abc513d4f115e91 to your computer and use it in GitHub Desktop.
Pulling the production database to your local development database (Rails + Postgres)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lib/tasks/db_pull.rake | |
# via https://martinschurig.com/posts/2015/02/pulling-production-database-to-local-machine-rails-task/ | |
namespace :db do | |
desc 'Pull production db to development' | |
task :pull => [:dump, :restore] | |
task :dump do | |
dumpfile = "#{Rails.root}/tmp/latest.dump" | |
puts 'PG_DUMP on production database...' | |
production = Rails.application.config.database_configuration['production'] | |
system "ssh [email protected] 'PGPASSWORD=\"#{production['password']}\" pg_dump -U postgres #{production['database']} -h #{production['host']} -F t' > #{dumpfile}" | |
puts 'Done!' | |
end | |
task :restore do | |
dev = Rails.application.config.database_configuration['development'] | |
dumpfile = "#{Rails.root}/tmp/latest.dump" | |
puts 'PG_RESTORE on development database...' | |
system "pg_restore --verbose --clean --no-acl --no-owner -h 127.0.0.1 -U #{dev['username']} -d #{dev['database']} #{dumpfile}" | |
puts 'Done!' | |
end | |
end |
Hi Will,
I'm trying to adapt your Gist to pull the production database of a server to a production database of another server. I use the following task but the restoration is not working:
task :restore do
dumpfile = "#{Rails.root}/tmp/latest.dump"
puts 'PG_RESTORE on production database...'
system "pg_restore --verbose --clean --no-acl --no-owner -h localhost -U user -d database_name #{dumpfile}"
puts 'Restore done!'
end
Do you know what's wrong here? In that case, what should be the host? The IP address of the server or "localhost"? Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Really useful, thanks!