DATABASE_URL examples for Rails DB connection strings

DATABASE_URL examples for Rails DB connection strings

Recently I’ve been configuring RailsEventStore to run tests on many databases on the Travis CI. We do it using DATABASE_URL environment variable but I couldn’t find good examples easily. So here they are.

PostgreSQL

DATABASE_URL=postgres://localhost/rails_event_store_active_record?pool=5

MySQL

DATABASE_URL=mysql2://root:@127.0.0.1/rails_event_store_active_record?pool=5

Sqlite in memory

DATABASE_URL=sqlite3::memory:

Code

ENV['DATABASE_URL'] ||= "postgres://localhost/rails_event_store_active_record?pool=5"

RSpec.configure do |config|
  config.around(:each) do |example|
    ActiveRecord::Base.establish_connection(ENV['DATABASE_URL'])
  end
end

Now, a plug 🔌. Join ARKADEMY.DEV and get access to our best courses: Rails Architect Masterclass, Anti-IF course, Blogging for busy programmers, Async Remote, TDD video class, Domain-Driven Rails video course and growing!

You might also like