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.
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)}
Check out these other useful embedded testing libraries for Clojure:
Copyright © 2024
Distributed under the Eclipse Public License, the same as Clojure.
Can you improve this documentation? These fine people already did:
Bill Hedworth & Emma GriffinEdit on GitHub
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |