Ruby on Rails provides many commands that you can run on the command line to perform common database tasks. The commands and what they do is listed below in a table.
You can generate the same list by running the following command in your terminal:
bin/rails -T db
Command | Description |
---|---|
bin/rails db:create | Create the database from DATABASE_URL or config/database.yml for the current RAILS_ENV (use db:create:all to create all databases in the config). Without RAILS_ENV or when RAILS_ENV is development, it defaults to creating the development and test databases, except when DATABASE_URL is present. |
bin/rails db:drop | Drop the database from DATABASE_URL or config/database.yml for the current RAILS_ENV (use db:drop:all to drop all databases in the config). Without RAILS_ENV or when RAILS_ENV is development, it defaults to dropping the development and test databases, except when DATABASE_URL is present. |
bin/rails db:encryption:init | Generate a set of keys for configuring Active Record encryption in a given environment |
bin/rails db:environment:set | Set the environment value for the database |
bin/rails db:fixtures:load | Load fixtures into the current environment's database |
bin/rails db:migrate | Migrate the database (options: VERSION=x, VERBOSE=false, SCOPE=blog) |
bin/rails db:migrate:down | Run the "down" for a given migration VERSION |
bin/rails db:migrate:redo | Roll back the database one migration and re-migrate up (options: STEP=x, VERSION=x) |
bin/rails db:migrate:status | Display status of migrations |
bin/rails db:migrate:up | Run the "up" for a given migration VERSION |
bin/rails db:prepare | Run setup if database does not exist, or run migrations if it does |
bin/rails db:reset | Drop and recreate all databases from their schema for the current environment and load the seeds |
bin/rails db:rollback | Roll the schema back to the previous version (specify steps w/ STEP=n) |
bin/rails db:schema:cache:clear | Clear a db/schema_cache.yml file |
bin/rails db:schema:cache:dump | Create a db/schema_cache.yml file |
bin/rails db:schema:dump | Create a database schema file (either db/schema.rb or db/structure.sql, depending on `ENV['SCHEMA_FORMAT']` or `config.active_record.schema_format`) |
bin/rails db:schema:load | Load a database schema file (either db/schema.rb or db/structure.sql, depending on `ENV['SCHEMA_FORMAT']` or `config.active_record.schema_format`) into the database |
bin/rails db:seed | Load the seed data from db/seeds.rb |
bin/rails db:seed:replant | Truncate tables of each database for current environment and load the seeds |
bin/rails db:setup | Create all databases, load all schemas, and initialize with the seed data (use db:reset to also drop all databases first) |
bin/rails db:version | Retrieve the current schema version number |
bin/rails test:db | Reset the database and run `bin/rails test` |