rails-db
Manage Rails application database connections
TLDR
Create databases, load the schema, and initialize with seed data
Access the database console
Create the databases defined in the current environment
Destroy the databases defined in the current environment
Run pending migrations
View the status of each migration file
Rollback the last migration
Fill the current database with data defined in db/seeds.rb
SYNOPSIS
rails db:
PARAMETERS
create
Creates the database as defined in `config/database.yml`.
drop
Drops the database.
environment
Sets the database environment to use. Defaults to the current Rails environment.
forward
Create the database specified by DATABASE_URL.
migrate
Runs pending migrations.
migrate:status
Shows the status of migrations.
prepare
Runs `db:drop`, `db:create`, and `db:migrate`.
reset
Runs `db:drop`, `db:create`, and `db:migrate`.
rollback
Rolls back the last migration.
seed
Seeds the database with data from `db/seeds.rb`.
setup
Runs `db:drop`, `db:create`, `db:schema:load`, and `db:seed`.
version
Retrieves the current schema version number.
console
Opens the database console specific to the configured database adapter (e.g., `psql`, `mysql`, `sqlite3`).
system:change
Performs a database system change.
encryption:key
Creates or shows encryption key.
DESCRIPTION
The `rails db` command in Ruby on Rails provides a convenient and consistent interface for interacting with your application's database. It simplifies common database tasks, such as running migrations, connecting to the database console, seeding data, and checking the database status. It abstracts away the underlying database adapter (e.g., PostgreSQL, MySQL, SQLite3), allowing you to use the same commands regardless of your database system.
This is primarily a wrapper around other commands with the intent to centralize and provide an easy to remember database interaction point. It enhances developer productivity by offering a streamlined workflow for managing the database directly from the command line, reducing the need to remember specific database client commands or navigate complex configurations.
The `rails db` command integrates with your Rails application's configuration, automatically loading the database settings from the `config/database.yml` file. This ensures that you are always interacting with the correct database environment (development, test, production, etc.) based on your current Rails environment.
CAVEATS
Ensure your `config/database.yml` file is correctly configured. Incorrect settings can lead to connection errors or data loss. Using drop on production databases is highly discouraged.
ENVIRONMENT VARIABLES
The `RAILS_ENV` environment variable determines which environment (`development`, `test`, `production`) is used. If not set, it defaults to `development`.
DATABASE CONFIGURATION
The `config/database.yml` file contains the database connection settings. It specifies the adapter, database name, username, password, and other connection parameters for each environment.
HISTORY
The `rails db` command was introduced as part of the Rails framework to provide a standardized way to interact with databases.
Earlier versions required the use of `rake db:
The command has evolved with new subcommands being added to cover various database administration tasks and has become an essential part of Rails development workflow.