- Notifications
You must be signed in to change notification settings - Fork90
yandex-qatools/postgresql-embedded
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Embedded PostgreSQL server provides a platform neutral way for running postgres binaries in unittests.This library is based onFlapdoodle OSS's embed process.
Sorry for any inconvinience, but this project needs active maintainers. If anyone is interested in becoming the maintainer - please let me (@smecsia) know.
Please be adviced that the main maintainer of this project has successfuly migrated to the use ofTest Containers project. This is the best possible alternative nowadays.
- 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
Add the following dependency to your pom.xml:
<dependency> <groupId>ru.yandex.qatools.embed</groupId> <artifactId>postgresql-embedded</artifactId> <version>2.10</version></dependency>
Add a line to build.gradle:
compile'ru.yandex.qatools.embed:postgresql-embedded:2.10'Here is the example of how to launch and use the embedded PostgreSQL instance
// starting PostgresfinalEmbeddedPostgrespostgres =newEmbeddedPostgres(V9_6);// predefined data directory// final EmbeddedPostgres postgres = new EmbeddedPostgres(V9_6, "/path/to/predefined/data/directory");finalStringurl =postgres.start("localhost",5432,"dbName","userName","password");// connecting to a running Postgres and feeding up the databasefinalConnectionconn =DriverManager.getConnection(url);conn.createStatement().execute("CREATE TABLE films (code char(5));");conn.createStatement().execute("INSERT INTO films VALUES ('movie');");// ... or you can execute SQL files...//postgres.getProcess().importFromFile(new File("someFile.sql"))// ... or even SQL files with PSQL variables in them...//postgres.getProcess().importFromFileWithArgs(new File("someFile.sql"), "-v", "tblName=someTable")// ... or even restore database from dump file//postgres.getProcess().restoreFromFile(new File("src/test/resources/test.binary_dump"))// performing some assertionsfinalStatementstatement =conn.createStatement();assertThat(statement.execute("SELECT * FROM films;"),is(true));assertThat(statement.getResultSet().next(),is(true));assertThat(statement.getResultSet().getString("code"),is("movie"));// close db connectionconn.close();// stop Postgrespostgres.stop();
Note that EmbeddedPostgres implementsjava.lang.AutoCloseable,which means that you can use it with atry-with-resourcesstatement (in Java >= 7) to have it automatically stopped.
You can specify the cached artifact store to avoid archives downloading and extraction (in case if a directory remains on every run).
finalEmbeddedPostgrespostgres =newEmbeddedPostgres();postgres.start(cachedRuntimeConfig("/path/to/my/extracted/postgres"));
Just configure your ownslf4j appenders. Here is the example of typicalsrc/test/resources/log4j.properties file:
#suppressinspection"UnusedProperty"forwholefilelog4j.rootLogger=DEBUG,stdout#reduceloggingforpostgresql-embeddedlog4j.logger.ru.yandex.qatools.embed=INFOlog4j.logger.de.flapdoodle.embed=INFO#Directlogmessagestostdoutlog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target=System.outlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-ddHH:mm:ss} %-5p %c{1}:%L - %m%nlog4j.throwableRenderer=org.apache.log4j.EnhancedThrowableRenderer
Pass the requiredIVersion interface implementation as a first argument of theEmbeddedPostgres object:
finalEmbeddedPostgrespostgres =newEmbeddedPostgres(() -> (IS_OS_WINDOWS) ?"9.6.2-2" :"9.6.2-1");
- A lot of issues have been reported for this library under Windows. Please try to use the suggested way of start up and usethe cached artifact storage (to avoid extraction of the archive as extraction is extremely slow under Windows):
postgres.start(cachedRuntimeConfig("C:\\Users\\vasya\\pgembedded-installation"));
- 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.
- 11.2: on Mac OS X and Windows 64 bit
- 10.7, 9.6.12, 9.5.16: on Linux, Windows, Mac OS X
- any custom version
About
Embedded PostgreSQL Server
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.