Coder Social home page Coder Social logo

postgresql-embedded's Introduction

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.19</version>
    </dependency>

Gradle

Add a line to build.gradle:

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

Howto

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

    // define of retrieve db name and credentials
    final String name = "yourDbname";
    final String username = "yourUser";
    final String password = "youPassword";

    // starting Postgres
    final PostgresStarter<PostgresExecutable, PostgresProcess> runtime = PostgresStarter.getDefaultInstance();
    final PostgresConfig config = PostgresConfig.defaultWithDbName(name, username, password);
    // pass info regarding encoding, locale, collate, ctype, instead of setting global environment settings
    config.getAdditionalInitDbParams().addAll(asList(
        "-E", "UTF-8",
        "--locale=en_US.UTF-8",
        "--lc-collate=en_US.UTF-8",
        "--lc-ctype=en_US.UTF-8"
    ));
    PostgresExecutable exec = runtime.prepare(config);
    PostgresProcess process = exec.start();
    
    // connecting to a running Postgres
    String url = format("jdbc:postgresql://%s:%s/%s?currentSchema=public&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');");

    // ... or you can execute SQL files...
    //pgProcess.importFromFile(new File("someFile.sql"))
    // ... or even SQL files with PSQL variables in them...
    //pgProcess.importFromFileWithArgs(new File("someFile.sql"), "-v", "tblName=someTable")
    
    // performing some assertions
    final Statement statement = conn.createStatement();
    assertThat(statement.execute("SELECT * FROM films;"), is(true));
    assertThat(statement.getResultSet().next(), is(true));

    // close db connection
    conn.close();

    // stop Postgres
    process.stop();

Important Notes

  • PostgreSQL server is known to not start under the privileged user (which means you cannot start it under root/Administrator of your system):

initdb must be run as the user that will own the server process, because the server needs to have access to the files and directories that initdb creates. Since the server cannot be run as root, you must not run initdb as root either. (It will in fact refuse to do so.) (link).

However some users have launched it successfully on Windows under Administrator, so you can try anyway.

  • It is no longer required to set up the LANG environment variable within your system, just pass that config as additionalInitDbParams.

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.

postgresql-embedded's People

Contributors

blove319 avatar donbeave avatar fdemilde avatar filius avatar lanwen avatar qatools-ci avatar rbraley avatar shtratos avatar smecsia avatar tteats avatar vbauer avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.