Does Ruby on Rails Work With PostgreSQL?
Ruby on Rails and PostgreSQL are a mature, battle-tested combination with first-class native support and excellent developer experience.
Quick Facts
How Ruby on Rails Works With PostgreSQL
Rails treats PostgreSQL as a primary database with full native support through the `pg` gem, which provides the connection adapter. The framework's Active Record ORM automatically maps Rails conventions to PostgreSQL schema generation, migrations, and query optimization. You get PostgreSQL-specific features like UUID support, JSON/JSONB columns, array types, full-text search, and range types as first-class Active Record citizens—not afterthoughts. Rails generators create `database.yml` with PostgreSQL as the default when you scaffold a new project with `rails new myapp --database=postgresql`. The developer experience is seamless: write migrations in Ruby, leverage PostgreSQL's advanced features through Active Record scopes and raw SQL when needed, and benefit from Rails' query batching and connection pooling. Most production Rails applications run on PostgreSQL because the combination provides excellent performance, reliability, and the ability to grow from startup to enterprise scale without hitting architectural limitations.
Best Use Cases
PostgreSQL Setup with Rails
rails new myapp --database=postgresql && cd myapp && bundle add pg# config/database.yml (auto-generated)
default: &default
adapter: postgresql
encoding: unicode
pool: 5
development:
<<: *default
database: myapp_development
# db/migrate/001_create_users.rb
class CreateUsers < ActiveRecord::Migration[7.0]
def change
create_table :users, id: :uuid do |t|
t.string :email, null: false
t.jsonb :metadata, default: {}
t.timestamps
end
add_index :users, :email, unique: true
add_index :users, :metadata, using: :gin
end
end
# app/models/user.rb
class User < ApplicationRecord
validates :email, uniqueness: true, presence: true
end
# Setup
rails db:create
rails db:migrateKnown Issues & Gotchas
UUID primary keys add minimal overhead but require explicit migration setup and may impact legacy schema imports
Fix: Use `id: :uuid` in migration DSL: `create_table :users, id: :uuid do |t|` for new projects; existing projects can selectively migrate to UUIDs
JSONB queries can become slow without proper indexing on frequently-queried JSON keys
Fix: Add GIN indices: `add_index :table, :json_column, using: :gin` and use PostgreSQL operators like `@>` (contains) in queries
Rails migrations lock tables during certain operations, causing downtime on large tables in production
Fix: Use `concurrent: true` for index creation, add `algorithm: :concurrently` flag, or use gems like `strong_migrations` to catch unsafe migrations
Connection pool exhaustion under load because Rails defaults to small pool sizes
Fix: Configure `database.yml` with appropriate `pool` size (typically 5-20 depending on concurrency model) and use PgBouncer for connection pooling at scale
Alternatives
- •Django with PostgreSQL: Python-based framework with equally strong PostgreSQL support, better for data science workflows
- •Node.js/Express with PostgreSQL: JavaScript fullstack with libraries like Sequelize or Prisma for ORM functionality
- •Laravel with PostgreSQL: PHP framework with Eloquent ORM providing similar Rails-like conventions
Resources
Related Compatibility Guides
Explore more compatibility guides