Back

Local Rails development with Postgres

Posted December 26, 2024

New Rails apps work out-of-the-box with SQLite, but if you want to use a Postgres instead, it takes a bit of local setup.

Here’s the simplest way I’ve found to get that experience with Postgres:

Use docker compose

Add this docker-compose.yml:

services:
  db:
    image: postgres:17.0-alpine3.20
    volumes:
      - db-data:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=password
volumes:
  db-data:

Spin up the database:

docker compose up -d

Use DATABASE_URL

Instead of messing with config/database.yml, just use DATABASE_URL, the same way you would in production:

# config/initializers/01_custom_app_setup.rb

Rails.application.config.before_configuration do
  ENV["DATABASE_URL"] ||= "postgresql://postgres:password@localhost:5432"
end