databaseyml
database.yml: The Definitive Guide to Rails Database Configuration
Key Takeaways
- database.yml is Rails' primary database configuration file, located at
config/database.ymlin every Rails project. - It defines connection parameters for all environments (development, test, production) in a single YAML document.
- Rails reads this file at boot time via
ActiveRecord::Base.establish_connection— changes require an application restart. - The default Rails 7+ template uses
primaryandprimary_replicanaming instead of the legacydefaultkey. - Never commit real credentials to version control; use
.envfiles or Rails credentials to manage secrets. - Multi-database support has been built into Rails since version 6.1, enabled directly through database.yml.
What Is database.yml?
database.yml is the central configuration file that tells a Rails application how to connect to its databases. It lives at config/database.yml and is generated automatically when you create a new Rails application with rails new.
Rails uses the YAML (YAML Ain't Markup Language) format to express connection settings in a human-readable structure. The Active Record ORM reads this file on initialization and establishes database connections based on the current RAILS_ENV environment variable.
Without a properly configured database.yml, Rails cannot communicate with any database. Every migration, query, and model operation depends on the connection parameters defined here.
Understanding database.yml Structure
Every database.yml file is organized into environment-specific sections. Each section contains keys that control how Rails connects to the database.
Core Keys Explained
| Key | Purpose | Example Value |
|---|---|---|
adapter |
Database engine to use | postgresql, mysql2, sqlite3 |
database |
Name of the target database | myapp_development |
host |
Server hosting the database | localhost, db.example.com |
username |
Authentication username | rails_user |
password |
Authentication password | secret_pass |
port |
TCP port for the connection | 5432 (PostgreSQL), 3306 (MySQL) |
pool |
Maximum number of concurrent connections | 5 to 25 |
timeout |
Connection timeout in milliseconds | 5000 |
migrations_paths |
Directory containing migration files | db/migrate |
schema_format |
Format for schema dumps | sql or ruby |
Default Template (Rails 7+)
default: &default
adapter: postgresql
encoding: unicode
pool: 25
host: localhost
port: 5432
username: <%= ENV['DB_USERNAME'] %>
password: <%= ENV['DB_PASSWORD'] %>
development:
<<: *default
database: myapp_development
test:
<<: *default
database: myapp_test
production:
<<: *default
database: myapp_production
url: <%= ENV['DATABASE_URL'] %>
Environment-Specific Configuration
Rails expects three default environments in database.yml, each pointing to a separate database instance:
development
The working database for daily application development. It should be local, fast, and frequently reset. Data here is disposable and typically recreated after each migration cycle.
test
An isolated database used exclusively during automated testing. Rails transactions wrap each test, so data from one test never leaks into another. The test database must be identical in schema to development.
production
The live database serving real users. Configuration here prioritizes reliability, connection pooling, security, and performance over convenience. Production credentials should never appear in plain text in this file.
Other Environments
Staging,-ci, and custom environments can be added as additional top-level keys. Rails resolves the active environment by reading the RAILS_ENV variable, which defaults to development.
Common database.yml Problems and How to Fix Them
Even experienced Rails developers hit configuration issues with database.yml. Below is a checklist of the most frequent problems and their resolutions.
Problem: "FATAL: role 'username' does not exist"
Cause: The database user specified in database.yml hasn't been created in PostgreSQL. Fix: Run createuser -s your_username in the terminal, or create the user through the psql console with CREATE USER your_username WITH PASSWORD 'secret';
Problem: "Can't connect to local PostgreSQL server"
Cause: The PostgreSQL service is not running, or the host/port is misconfigured. Fix: Start the service (brew services start postgresql on macOS), then verify the host and port values match your installation.
Problem: "ActiveRecord::NoDatabaseError"
Cause: The target database doesn't exist yet. Fix: Run rails db:create to create all databases defined in database.yml, or create the specific database manually with your database client.