Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Rails database tasks are run with bin/rails db:... in current Rails documentation; “Rake database commands” is the older, still-common name for this task family. For routine schema changes, run bin/rails db:migrate. For a clean rebuild, choose between db:reset, which loads the current schema dump, and db:migrate:reset, which replays migration history. Check the target environment before any destructive task.

What are Rails database commands?

They are Rails command-line tasks for creating, changing, inspecting, and initializing an application database. The Rails command-line guide calls db:migrate one of the most frequently run commands: it runs migrations that have not yet been applied. The current command form is bin/rails db:..., even though developers may still refer to them as Rake database tasks. See the Rails Command Line guide.

Run these commands from the Rails application directory. They operate on the current environment unless a task explicitly says otherwise; confirm which database that environment points to before dropping, resetting, or replanting data.

Which command should you use?

Goal Command Effect and caution
Apply pending schema changes bin/rails db:migrate Runs all pending migrations; the routine forward migration path.
Undo the latest migration bin/rails db:rollback Reverses the previous migration if it is reversible. Use STEP=n to roll back multiple steps.
Rebuild from the checked-in schema bin/rails db:reset Drops and recreates databases from db/schema.rb or db/structure.sql, then loads seeds. Destructive.
Rebuild by replaying migration history bin/rails db:migrate:reset Drops the database and runs all migrations from the beginning. Destructive; can reveal problems in migration history.
Initialize without dropping first bin/rails db:setup Creates databases, loads schemas, and initializes seed data. It does not drop databases first.
Load seed data bin/rails db:seed Runs seed code in db/seeds.rb; its behavior may depend on the environment.
Clear table data and reload seeds bin/rails db:seed:replant Truncates current-environment tables and reloads seed data. Destructive.

How to create or update a database

For a new local application database, create it and then apply migrations:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  1. Run bin/rails db:create to create the database for the current environment.
  2. Run bin/rails db:migrate to apply pending migrations.
  3. Optionally run bin/rails db:seed to load the application’s seed data.

The Rails guide also lists bin/rails db:create:all and bin/rails db:drop:all for all environments. Use the all-environments variants only when that broader scope is intentional.

How to roll back or target a migration

Use the least broad operation that accomplishes the task. A rollback addresses the most recent migration, while version-specific tasks let you run a particular migration direction.

  • bin/rails db:rollback reverses the most recent migration; bin/rails db:rollback STEP=n rolls back multiple steps.
  • bin/rails db:migrate:up VERSION=x runs the up direction for migration version x.
  • bin/rails db:migrate:down VERSION=x runs the down direction for version x. Inspect dependent changes before reversing a targeted migration.
  • bin/rails db:migrate:redo rolls back one migration and runs it again; it can help when revising a local migration.

Not every migration can be reversed automatically. If rollback fails, inspect the migration for irreversible operations and consider whether restoring data or writing a corrective migration is safer than forcing a reversal.

How to check migration and schema state

  • bin/rails db:migrate:status lists migrations as up or down. Use it to identify unapplied migrations or investigate deployment differences.
  • bin/rails db:version reports the current schema version.
  • bin/rails db:schema:dump writes the schema representation; bin/rails db:schema:load loads it. Rails uses db/schema.rb or db/structure.sql as the schema dump, depending on the application’s configuration.

What is the difference between db:reset and db:migrate:reset?

db:reset recreates the database from the current schema dump and loads seeds. It is generally the quicker route to a fresh database matching that dump, but it does not verify that every migration can run from scratch.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

db:migrate:reset drops the database and replays the migration history from the beginning. That can expose drift when migrations have been edited, reordered, or removed. Both are destructive; use them only when losing the current database contents is acceptable.

How do you load seeds after a migration?

Run bin/rails db:migrate, followed by bin/rails db:seed. Migrations change the schema; the seed task separately runs db/seeds.rb. If the intended operation is a complete reset, bin/rails db:reset also loads seeds as part of rebuilding from the schema dump. Seed scripts may be environment-sensitive, so verify the target before running them.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

How to choose a safe workflow

  • Normal development or deployment: run bin/rails db:migrate to apply pending changes.
  • Undo a recent reversible change: run bin/rails db:rollback, then check bin/rails db:migrate:status.
  • Set up a database without removing its existing contents first: use bin/rails db:setup.
  • Intentionally replace the database using the schema dump and seeds: use bin/rails db:reset.
  • Check that the full migration history still builds a database: use bin/rails db:migrate:reset, only where dropping the database is safe.

For any destructive command, confirm the Rails environment and database configuration first. A command that is appropriate for a disposable local database can delete data you need if run against another environment.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.