Awesome
pg-embedded is not affected by the discovered xz supply chain attack! All releases of pg-embedded have shipped with xz 1.9 or earlier, which predates the attack. It was never shipped with a version that contains potentially compromising commits.
Embedded Postgres for Java
Start a PostgreSQL server for unit tests or local development.
Please check out the full documentation site!
- PostgreSQL version 11+ (see https://www.postgresql.org/docs/ for all supported versions of PostgreSQL).
- Binaries loaded from Maven Central (using the Zonky embedded Postgres Binaries) or locally installed.
- Multiple Database servers
- Multiple databases on a single database server
- Flyway support for Database preparation
Manage a PostgreSQL server
try (EmbeddedPostgres pg = EmbeddedPostgres.defaultInstance();
Connection c = pg.createDefaultDataSource().getConnection();
Statement s = c.createStatement()) {
try (ResultSet rs = s.executeQuery("SELECT 1")) {
assertTrue(rs.next());
assertEquals(1, rs.getInt(1));
assertFalse(rs.next());
}
}
Manage Databases
try (DatabaseManager manager = DatabaseManager.multiDatabases()
.withInstancePreparer(EmbeddedPostgres.Builder::withDefaults)
.build()
.start();
Connection c = manager.getDatabaseInfo().asDataSource().getConnection();
Statement s = c.createStatement()) {
try (ResultSet rs = s.executeQuery("SELECT 1")) {
assertTrue(rs.next());
assertEquals(1, rs.getInt(1));
assertFalse(rs.next());
}
}
Unit Tests with JUnit 5
@RegisterExtension
public static EmbeddedPgExtension pg = MultiDatabaseBuilder.instanceWithDefaults().build();
@Test
public void simpleTest() throws SQLException {
try (Connection c = pg.createDataSource().getConnection();
Statement s = c.createStatement()) {
try (ResultSet rs = s.executeQuery("SELECT 1")) {
assertTrue(rs.next();
assertEquals(1, return rs.getInt(1));
}
}
}