"pg_dump: invalid option -- i" when migrating

Unlikely there is a fix as it's not a security issue. Even if it was, I'm not sure they are patching 3.x anymore.

The problem is in the db:structure:dump task here:

https://github.com/rails/rails/blob/v3.2.22.2/activerecord/lib/active_record/railties/databases.rake#L428

Easiest thing is to copy that task (413 - 448) and put it into your own lib/tasks directory, wrap a namespace db around it, tweak the pg_dump command (remove -i) and your task should override the built in task.


I ran into this issue as well with Rails 3.2.22. It looks like this was fixed in 4.2.5, but for our situation, upgrading Rails was not very practical.

After considering some options, I ended up down the path of overriding the default rake task db:structure:dump which is getting called after db:migrate.

I created a file tasks/database.rake and hacked together bits and pieces from different ActiveRecord methods to create a new db:structure:dump task. Now this new task is called instead of the default when db:migrate, etc is executed.

Rake::Task["db:structure:dump"].clear
namespace :db do
  namespace :structure do
    desc "Overriding the task db:structure:dump task to remove -i option from pg_dump to make postgres 9.5 compatible"
    task dump: [:environment, :load_config] do
      config = ActiveRecord::Base.configurations[Rails.env]
      set_psql_env(config)
      filename =  File.join(Rails.root, "db", "structure.sql")
      database = config["database"]
      command = "pg_dump -s -x -O -f #{Shellwords.escape(filename)} #{Shellwords.escape(database)}"
      raise 'Error dumping database' unless Kernel.system(command)

      File.open(filename, "a") { |f| f << "SET search_path TO #{ActiveRecord::Base.connection.schema_search_path};\n\n" }
      if ActiveRecord::Base.connection.supports_migrations?
        File.open(filename, "a") do |f|
          f.puts ActiveRecord::Base.connection.dump_schema_information
          f.print "\n"
        end
      end
      Rake::Task["db:structure:dump"].reenable
    end
  end

  def set_psql_env(configuration)
    ENV['PGHOST']     = configuration['host']          if configuration['host']
    ENV['PGPORT']     = configuration['port'].to_s     if configuration['port']
    ENV['PGPASSWORD'] = configuration['password'].to_s if configuration['password']
    ENV['PGUSER']     = configuration['username'].to_s if configuration['username']
  end
end

This code was created specifically for our project, so if you have any other custom configurations set like db_dir, you will need to adjust accordingly.