Skip to content

Instantly share code, notes, and snippets.

@willwright82
Created July 6, 2016 08:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willwright82/930180ea763ab5558abc513d4f115e91 to your computer and use it in GitHub Desktop.
Save willwright82/930180ea763ab5558abc513d4f115e91 to your computer and use it in GitHub Desktop.
Pulling the production database to your local development database (Rails + Postgres)
# 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 user@server.tld '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
@nicolrx
Copy link

nicolrx commented Dec 13, 2020

Really useful, thanks!

@nicolrx
Copy link

nicolrx commented Apr 20, 2021

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