begin # Get the hash (i.e. parsed) representation of database.yml databases = Rails.configuration.database_configuration # Get a fancier AR-specific representation of database.yml resolver = ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver.new(databases) # Get one specific database from our list of databases in database.yml. pick any database identifier (:development, :user_shard1, etc) spec = resolver.spec(:development) # Make a new pool for the database we picked pool = ActiveRecord::ConnectionAdapters::ConnectionPool.new(spec) # Use the pool # This is thread-safe, ie unlike ActiveRecord's establish_connection, it won't leak to other threads pool.with_connection { puts Post.count } rescue => ex puts ex, ex.backtrace ensure pool.disconnect! end