Home

Awesome

Main Build


pg-embedded is not affected by the xz supply chain attack attempt! All releases of pg-embedded have shipped with xz 1.10 or earlier, which either predates the attack (1.9) or was released after the attack was mitigated (1.10). pg-embedded 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!

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));
        }
    }
}