logo-header

BLOG


Seeding a Production Postgresql Database with Capistrano

Header

September, 04, 2020 by Andrew Farmer in Web Development



So, you want to take the quick way out and input data from your seed.rb file into your newly deployed Ruby on Rails website.
If you're using DigitalOcean as your host, this is especially useful.

While this is well known for being bad practice and shouldn’t be done; if you would like to seed a production postgresql database with Capistrano using your seeds.rb, place the following code into your config/deploy.rb file and upload it to your web server.

namespace :deploy do
  task :seed do
    on primary fetch(:migration_role) do
      within release_path do
        with rails_env: fetch(:rails_env)  do
          execute :rake, 'db:seed'
        end
      end
    end
  end
end

Then submit

bundle exec cap production deploy:seed

in your local terminal.

This will seed your production database with information contained in the seeds.rb file and you will see the result on your website.

IMPORTANT NOTE: Remember to remove the seeds.rb file from the webserver and Github afterwards for obvious security reasons. In the future, you should also put seeds.rb into the .gitignore file found in your project. If you leave this file on your Github or Web server, you open yourself up to potential malicious attacks