Ruby on Rails 8 cheatsheet

Ruby on Rails 8: Database

8 common Ruby on Rails 8 tasks.

Create the databases

Create the development and test databases.

bin/rails db:create

Run migrations

Apply pending migrations and update db/schema.rb.

bin/rails db:migrate

Check migration status

Show which migrations have been run.

bin/rails db:migrate:status

Roll back the last migration

Undo the most recent migration.

bin/rails db:rollback

Caution: Rolling back can drop columns or tables and their data.

Load seed data

Load the data in db/seeds.rb into the database.

bin/rails db:seed

Prepare the database

Create the database if it is missing, then migrate. Safe to run repeatedly.

bin/rails db:prepare

Drop and recreate the database

Drop, recreate, load the schema and seed.

bin/rails db:reset

Caution: Deletes all data in the database for the current environment.

Open the database shell

Open psql, mysql or sqlite3, depending on your configuration.

bin/rails dbconsole

Was this page helpful?