3

What is the easiest way to initialize some lightweight PostgreSQL instance (probably in some temp directory) from java code. I need some temporary database for running the test; so best way will be to create/drop that cluster (database) before/after running the tests.

Is there some java library for that? Is it possible with pure java code, without external scripts?

My stack is Java, Maven, jetty, TestNG, PostgreSQL.

5
  • 2
    github.com/yandex-qatools/postgresql-embedded Commented Nov 16, 2015 at 14:10
  • Maybe you just need DBUnit Commented Nov 16, 2015 at 14:18
  • @Mr_Thorynque: DBUnit still needs a database to connect to. Commented Nov 16, 2015 at 14:25
  • Fairly common question. Nobody I know of has written a Java library for it. (Everyone who asks for one seems unwilling to write one). You'll need to invoke initdb and postgres commands via ProcessBuilder etc. Pretty easy really. Commented Nov 16, 2015 at 15:16
  • Hm, I take it back. There's github.com/adrianboimvaser/postgresql-maven-plugin . Commented Nov 16, 2015 at 15:17

2 Answers 2

2

Use the PostgreSQL maven plugin, which is designed for the purpose.

You'll need to tell it where PostgreSQL's binaries are if it can't find them on the PATH.


If you want to roll your own you'll need to:

  • Invoke initdb via ProcessBuilder to create a temp data directory.
  • Make any necessary changes to pg_hba.conf or generate a new one
  • Invoke postgres via ProcessBuilder to start PostgreSQL

Your ProcessBuilder must set up the PATH environment variable so PostgreSQL can find its other binaries.

You don't have to generate a custom postgresql.conf, as you can just pass parameters on the command line, e.g.

postgres -D some-datadir -c port=5599 -c listen_addresses='127.0.0.1'

etc. But if you want, you can just append a single line to the default postgresql.conf like

include 'myapp-test.conf'

then generate/copy a myapp-test.conf file containing all the PostgreSQL config settings you want. This can be easier if you have lots of settings and/or complex ones due to command line length limits, and it can be more readable/debuggable.

Do not use the default port when starting PostgreSQL for testing. Always override the port.

Sign up to request clarification or add additional context in comments.

Comments

0

depends on what exactly do you need. only the database to be up and running? or also manage data between tests?

  1. testcontainers will help you start and stop db.

  2. dbunit will help you prepare data for your test.

    cons:

    • a lot of work is required to create and maintain schema and data. especially when your project is in a intensive development stage.
    • it's another abstraction layer so if suddenly you want to use some db feature that is unsupported by this tool, it may be difficult to test it
  3. testegration - intents to provide you full, ready to use and extensible lifecycle (disclosure: i'm a creator).

    cons:

    • free only for small projects
    • very young project

you can also fill the gaps on your own (ProcessBuilder, executing custom scripts). as always it's a trade: time vs money

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.