1

I am working on a project for school and need to replace hsqldb with postgresql in a maven project.

Currently we start the hsql Server by running

mvn exec:java -P hsqldb

To my knowlege this looks up the hsqldb profile in the pom.xml

<profile>
  <id>hsqldb</id>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.2.1</version>
        <executions>
          <execution>
            <goals>
              <goal>java</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <mainClass>org.hsqldb.server.Server</mainClass>
          <arguments>
            <argument>-database.0</argument>
            <argument>db/name</argument>
            <argument>-dbname.0</argument>
            <argument>name</argument>
          </arguments>
        </configuration>
      </plugin>
    </plugins>
  </build>
</profile>

So I need to relace this part with a postgres setup, I already added postgres as a dependency.

<mainClass>ServerClass</mainClass>

is my biggest problem since I cant find the main server class in the postgresql jar

Konzi
  • 13
  • 1
  • 4
  • You can't start a Postgres server like that. You need to first initialize ("prepare") a data directory (using `initdb`) and then use `pg_ctl` to start the actual server process. This is well documented in the Postgres manual –  Jun 04 '14 at 11:13
  • Thanks, I was on the wrong track and will try to initialize postgresql seperatly – Konzi Jun 04 '14 at 11:17
  • Out of curiosity: Why do you want to start a db server from maven? – Aleksandr M Jun 04 '14 at 11:22
  • The setup was provided by the teachers. I think it was done cause of its simplicity, every team member runs one bat file to setup the database, the server and start the main class of the project – Konzi Jun 04 '14 at 11:33

2 Answers2

7

PostgreSQL isn't an in-memory embeddable server. It can't just be loaded as a library and run.

You'll need some test harness control code to:

  • initdb a temporary datadir

  • Edit postgresql.conf as required by appending lines, or by enabling an include_dir and dropping files in it. If you only need a trivial config you might be able to skip this step; things like the port can be set via environment variables, and many others can be set with -c flags to the server startup command.

  • generate a random port number to use

  • Launch the server - run postgres -D the_datadir a new server instance using Java's Process management. You can pass custom config values with -c and can also set environment variables to control behaviour.

  • Connect over JDBC and CREATE DATABASE then run your test setup

  • Run your tests

  • Stop the server by killing the process you launched.

  • Delete the datadir

You'll want to do the process management in a class with appropriate temp directory handling, cleanup on unclean exit (kill the Pg server and delete the datadir on exception), etc.

I'll be surprised if you can't find canned test harness code to borrow for this. I wonder if we should add some to PgJDBC and bundle it in the driver? If you find anything good, or write anything good, please ping me by commenting on this answer and I'll consider it for inclusion as a utility class in PgJDBC.

That said, it is much more common to instead run your tests in a newly created test database in an existing PostgreSQL instance that already runs on your server. You just configure your test suite with a PostgreSQL username/password/host/port/database (or let it connect to the postgres database and CREATE and DROP the database). After all, some configuration is always going be required: installing or compiling PostgreSQL, making sure the binaries are on the PATH, etc.

Another option is to use Amazon RDS: Use the AWS RDS APIs to launch a new PostgreSQL instance and use it for your tests. This probably isn't practical unless your tests run for a long time due to the setup time and minimum runtime, but it's another option, as is the similar service at Heroku.

Craig Ringer
  • 307,061
  • 76
  • 688
  • 778
0

I believe you need this:

<mainClass>org.postgresql.Driver</mainClass>
d1ll1nger
  • 1,571
  • 12
  • 16