When writing tests, I’ve often simulated Postgres using in-memory Java SQL databases like HSQL or H2. In general this has been a frustrating experience. While both have modes which purport to offer syntax compatibility with Postgres, neither has true feature parity, and it’s always a struggle to “test what you fly.” So you can imagine how excited I was to discover just how easy it is to embed1 a real, no-kidding Postgres server in your Clojure application, thanks to OpenTable’s Embedded Postgres component.
I originally discovered this component via the lein-postgres plugin, but it didn’t really mesh with my REPL-heavy workflow, so I decided to manage the embedded Postgres instance from within my own code. This turns out to be very straightforward.
To begin, add the component’s coordinates to profile.clj (probably in your dev or test profile):
And import the class in your
The Postgres process runs as a child of the Java process, and there are no external dependencies. By default, it empties the data directory that it uses each time it starts up, although this is configurable, as are the data directory location, port number, etc. (see the OpenTable documentation). You can even connect to the server with the psql client.
I ended up using clojure.test fixtures to manage the lifecycle of the database server:
You can even directly load schema dumps generated with pg_dump–try that with HSQL!
As a die-hard Postgres fan I’ve found it liberating to be able to really take advantage of Postgres’s advanced features without giving up solid tests–tests which exercise not only my code but my understanding of how Postgres will behave.
I’m playing a little fast and loose with the word “embedded” here–in fact, Postgres runs as a separate child process, so it’s not truly embedded in the sense that SQLite is embedded. My terminology follows OpenTable’s description of its component. ↩