A Clojure library that provides embedded PostgreSQL for testing and development purposes. Based on zonkyio/embedded-postgres, this library makes it easy to spin up and tear down PostgreSQL instances in your Clojure applications.
- Simple API for starting and stopping PostgreSQL instances
- Configurable port and logging options
- Support for different PostgreSQL versions and architectures
- Perfect for integration testing and local development
- Automatic cleanup of resources
Add the following dependency to your project.clj
:
[org.clojars.bigsy/pg-embedded-clj "latest-version"]
By default, this library uses the standard Zonky PostgreSQL version. For most use cases, this default version is sufficient.
To use a specific PostgreSQL version or architecture, include an additional dependency from zonky-postgres-binaries. For example:
;; For PostgreSQL 17 on Apple Silicon
[io.zonky.test.postgres/embedded-postgres-binaries-darwin-arm64v8 "17.0.0"]
(require '[pg-embedded-clj.core :refer :all])
;; Start PostgreSQL with default settings (port 5432)
(init-pg)
;; Start PostgreSQL with custom configuration
(init-pg {:port 5433
:log-redirect "postgres.log"})
;; Stop the PostgreSQL instance
(halt-pg!)
The library provides convenient fixtures for integration testing:
(ns your-test-namespace
(:require [clojure.test :refer :all]
[pg-embedded-clj.core :refer [with-pg-fn default-config]]))
;; Using the fixture with custom configuration
(defn test-fixture
[f]
(with-pg-fn (merge default-config
{:port 54321
:log-redirect "test.log"})
f))
(use-fixtures :once test-fixture)
;; Your tests here
(deftest your-integration-test
(testing "Database operations"
;; Your test code here
))
;; For ad-hoc testing blocks
(deftest another-test
(with-pg default-config
;; Your test code here
))
The following configuration options are available:
{:port 5432 ; PostgreSQL port (default: 5432)
:log-redirect nil ; Log file path (default: nil for no logging)
:data-dir nil} ; Custom data directory (optional)
Check out these other useful embedded testing libraries for Clojure:
- dynamo-embedded-clj - Embedded DynamoDB
- redis-embedded-clj - Embedded Redis
- s3-clj - Embedded S3
- elasticmq-clj - Embedded ElasticMQ
- sns-clj - Embedded SNS
Copyright © 2024
Distributed under the Eclipse Public License, the same as Clojure.