Managing databases in Rails can seem overwhelming with all the available commands. This comprehensive guide will walk you through every essential Rails database command, from basic operations to complex real-world scenarios.
Basic Database Commands
Core Database Operations
# Create the database
rails db:create
# Drop (delete) the database
rails db:drop
# Run pending migrations
rails db:migrate
# Rollback the last migration
rails db:rollback
# Rollback multiple migrations
rails db:rollback STEP=3
Schema Management
# Load current schema into database
rails db:schema:load
# Dump current database structure to schema.rb
rails db:schema:dump
# Load structure from structure.sql (for complex databases)
rails db:structure:load
# Dump database structure to structure.sql
rails db:structure:dump
Seed Data
# Run the seed file (db/seeds.rb)
rails db:seed
Combined Commands: The Powerhouses
rails db:setup
What it does: Sets up database from scratch
rails db:setup
Equivalent to:
rails db:create
rails db:schema:load # Loads from schema.rb
rails db:seed
When to use:
- First time setting up project on new machine
- Fresh development environment
- CI/CD pipeline setup
rails db:reset
What it does: Nuclear option – completely rebuilds database
rails db:reset
Equivalent to:
rails db:drop
rails db:create
rails db:schema:load
rails db:seed
When to use:
- Development when you want clean slate
- After major schema changes
- When your database is corrupted
⚠️ Warning: Destroys all data!
rails db:migrate:reset
What it does: Rebuilds database using migrations
rails db:migrate:reset
Equivalent to:
rails db:drop
rails db:create
rails db:migrate # Runs all migrations from scratch
When to use:
- Testing that migrations run cleanly
- Debugging migration issues
- Ensuring migration sequence works
Advanced Database Commands
Migration Management
# Rollback to specific migration
rails db:migrate:down VERSION=20240115123456
# Re-run specific migration
rails db:migrate:up VERSION=20240115123456
# Get current migration version
rails db:version
# Check migration status
rails db:migrate:status
Database Information
# Show database configuration
rails db:environment
# Validate database and pending migrations
rails db:abort_if_pending_migrations
# Check if database exists
rails db:check_protected_environments
Environment-Specific Commands
# Run commands on specific environment
rails db:create RAILS_ENV=production
rails db:migrate RAILS_ENV=staging
rails db:seed RAILS_ENV=test
Real-World Usage Scenarios
Scenario 1: New Developer Onboarding
# New developer joins the team
git clone project-repo
cd project
bundle install
# Set up database
rails db:setup
# Or if you prefer running migrations
rails db:create
rails db:migrate
rails db:seed
Scenario 2: Production Deployment
# Safe production deployment
rails db:migrate RAILS_ENV=production
# Never run these in production:
# rails db:reset ❌ Will destroy data!
# rails db:schema:load ❌ Will overwrite everything!
Scenario 3: Development Workflow
# Daily development cycle
git pull origin main
rails db:migrate # Run any new migrations
# If you have conflicts or issues
rails db:rollback # Undo last migration
# Fix migration file
rails db:migrate # Re-run
# Major cleanup during development
rails db:reset # Nuclear option
Scenario 4: Testing Environment
# Fast test database setup
rails db:schema:load RAILS_ENV=test
# Or use the test-specific command
rails db:test:prepare
Environment-Specific Best Practices
Development Environment
# Liberal use of reset commands
rails db:reset # ✅ Safe to use
rails db:migrate:reset # ✅ Safe to use
rails db:setup # ✅ Safe for fresh start
Staging Environment
# Mirror production behavior
rails db:migrate RAILS_ENV=staging # ✅ Recommended
rails db:seed RAILS_ENV=staging # ✅ If needed
# Avoid
rails db:reset RAILS_ENV=staging # ⚠️ Use with caution
Production Environment
# Only safe commands
rails db:migrate RAILS_ENV=production # ✅ Safe
rails db:rollback RAILS_ENV=production # ⚠️ With backup
# Never use in production
rails db:reset RAILS_ENV=production # ❌ NEVER!
rails db:drop RAILS_ENV=production # ❌ NEVER!
rails db:schema:load RAILS_ENV=production # ❌ NEVER!
Pro Tips and Gotchas
Migration vs Schema Loading
# For existing databases with data
rails db:migrate # ✅ Incremental, safe
# For fresh databases
rails db:schema:load # ✅ Faster, clean slate
Data vs Schema
Remember that some operations preserve data differently:
db:migrate: Preserves existing data, applies incremental changesdb:schema:load: Loads clean schema, no existing datadb:reset: Destroys everything, starts fresh
Common Workflow Commands
# The "fix everything" development combo
rails db:reset && rails db:migrate
# The "fresh start" combo
rails db:drop db:create db:migrate db:seed
# The "production-safe" combo
rails db:migrate db:seed
Quick Reference Cheat Sheet
| Command | Use Case | Data Safety | Speed |
|---|---|---|---|
db:migrate | Incremental updates | ✅ Safe | Medium |
db:setup | Initial setup | ✅ Safe (new DB) | Fast |
db:reset | Clean slate | ❌ Destroys all | Fast |
db:migrate:reset | Test migrations | ❌ Destroys all | Slow |
db:schema:load | Fresh schema | ❌ No data migration | Fast |
db:seed | Add sample data | ✅ Additive | Fast |
Conclusion
Understanding Rails database commands is crucial for efficient development and safe production deployments. Start with the basics (db:create, db:migrate, db:seed), get comfortable with the combined commands (db:setup, db:reset), and always remember the golden rule: be very careful with production databases!
The key is knowing when to use each command:
- Development: Feel free to experiment with
db:resetand friends - Production: Stick to
db:migrateand always have backups - Team collaboration: Use migrations to keep everyone in sync
Remember: migrations tell the story of how your database evolved, while schema files show where you ended up. Both are important, and now you know how to use all the tools Rails gives you to manage them effectively.