testcontainers-postgres simplifies the creation and management of Postgres containers for testing purposes using the Testcontainers library.
Add the library to the test section in your application's Gemfile:
group :test do
gem "testcontainers-postgres"
endAnd then execute:
$ bundle installOr install it yourself as:
$ gem install testcontainers-postgresTo use the library, you first need to require it:
require "testcontainers/postgres"Create a new instance of the Testcontainers::PostgresContainer class:
container = Testcontainers::PostgresContainer.newThis creates a new container with the default Postgres image, user, password, and database. You can customize these by passing arguments to the constructor:
container = Testcontainers::PostgresContainer.new("postgres:11", username: "custom_user", password: "custom_pass", database: "custom_db")Start the container:
container.startStop the container when you're done:
container.stopOnce the container is running, you can obtain the connection details using the following methods:
host = container.host
port = container.first_mapped_portOr, you can generate a full database URL:
database_url = container.database_urlUse this URL to connect to the Postgres container using your preferred postgres client library.
You can also customize the container using the following methods:
container.with_database("custom_db")
container.with_username("custom_user")
container.with_password("custom_pass")Here's a complete example of how to use testcontainers-postgres to create a container, connect to it, and run a simple query:
require "testcontainers/postgres"
require "pg"
container = Testcontainers::PostgresContainer.new
container.start
client = PG.connect(container.database_url)
result = client.exec("SELECT 1 AS number")
result.each do |row|
puts row.inspect
end
client.close
container.stopThis example creates a Postgres container, connects to it using the pg gem, runs a simple SELECT 1 as number query, and then stops the container.
You can manage the container in the before(:suite) / after(:suite) blocks in your spec_helper.rb:
RSpec.configure do |config|
# This helps us to have access to the `RSpec.configuration.postgres_container` without using global variables.
config.add_setting :postgres_container, default: nil
config.before(:suite) do
config.postgres_container = Testcontainers::PostgresContainer.new.start
ENV["DATABASE_URL"] = config.postgres_container.database_url # or you can expose it to a fixed port and use database.yml for configuration
end
config.after(:suite) do
config.postgres_container&.stop
config.postgres_container&.remove
end
endBug reports and pull requests are welcome on GitHub at https://github.com/testcontainers/testcontainers-ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the Testcontainers project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.