/postgresql-embedded

Embedded PostgreSQL Server

Primary LanguageJavaOtherNOASSERTION

Embedded PostgreSQL Server

Maven Central Build status covarage

Embedded PostgreSQL server provides a platform neutral way for running postgres binaries in unittests. This library is based on Flapdoodle OSS's embed process.

Please consider using the embedded-services project as well.

Motivation

  • It's much easier than installing specific version manually
  • You can choose the version right from the code
  • You can start your development environment with the PostgreSQL embedded with the single command

Maven

Add the following dependency to your pom.xml:

    <dependency>
        <groupId>ru.yandex.qatools.embed</groupId>
        <artifactId>postgresql-embedded</artifactId>
        <version>1.11</version>
    </dependency>

Gradle

Add a line to build.gradle:

    compile 'ru.yandex.qatools.embed:postgresql-embedded:1.10'

Howto

Here is the example of how to launch and use the embedded PostgreSQL instance

    // starting Postgres
    PostgresStarter<PostgresExecutable, PostgresProcess> runtime = PostgresStarter.getDefaultInstance();
    final PostgresConfig config = PostgresConfig.defaultWithDbName("test");
    PostgresExecutable exec = runtime.prepare(config);
    PostgresProcess process = exec.start();
    
    // connecting to a running Postgres
    String url = format("jdbc:postgresql://%s:%s/%s?user=%s&password=%s",
            config.net().host(),
            config.net().port(),
            config.storage().dbName(),
            config.credentials().username(),
            config.credentials().password()
    );
    Connection conn = DriverManager.getConnection(url);
    
    // feeding up the database
    conn.createStatement().execute("CREATE TABLE films (code char(5));");
    conn.createStatement().execute("INSERT INTO films VALUES ('movie');");
    
    // performing some assertions
    final Statement statement = conn.createStatement();
    assertThat(statement.execute("SELECT * FROM films;"), is(true));
    assertThat(statement.getResultSet().next(), is(true));
                
    // stopping Postgres
    conn.close();
    process.stop();

Supported Versions

Versions: 9.5.0, 9.4.4, 9.4.1, 9.3.5, 9.2.4, any custom Support for Linux, Windows and MacOSX.