Class EmbeddedPostgres

java.lang.Object
de.softwareforge.testing.postgres.embedded.EmbeddedPostgres
All Implemented Interfaces:
AutoCloseable

public final class EmbeddedPostgres extends Object implements AutoCloseable
Manages an embedded PostgreSQL server instance.
  • Field Details

    • DEFAULT_POSTGRES_VERSION

      public static final String DEFAULT_POSTGRES_VERSION
      The version of postgres used if no specific version has been given.
      See Also:
  • Method Details

    • defaultInstance

      @NonNull public static EmbeddedPostgres defaultInstance() throws IOException
      Returns an instance that has been started and configured. The EmbeddedPostgres.Builder.withDefaults() configuration has been applied.
      Throws:
      IOException
    • builderWithDefaults

      @NonNull public static EmbeddedPostgres.Builder builderWithDefaults()
      Returns a builder with default EmbeddedPostgres.Builder.withDefaults() configuration already applied.
    • forVersionCheck

      public static EmbeddedPostgres forVersionCheck() throws IOException
      This returns an EmbeddedPostgres instance that can be solely used for version checking. It has not been booted and will not work for any other things but executing getPostgresVersion(). This is a performance optimization for code that needs to do version checking and does not want to pay the penalty of spinning up and shutting down an instance.
      Returns:
      An unstarted EmbeddedPostgres instance.
      Throws:
      IOException - Could not create the instance.
      Since:
      4.1
    • builder

      @NonNull public static EmbeddedPostgres.Builder builder()
    • createTemplateDataSource

      @NonNull public DataSource createTemplateDataSource() throws SQLException
      Creates a DataSource object that connects to the standard system database.

      The standard system database is the template1 database.

      Any modification to this database will be propagated to any new database that is created with CREATE DATABASE... unless another database is explicitly named as the template..

      Throws:
      SQLException
    • createDefaultDataSource

      @NonNull public DataSource createDefaultDataSource() throws SQLException
      Creates a DataSource object that connects to the default database.

      The default database is the postgres database.

      Throws:
      SQLException
    • createDataSource

      @NonNull public DataSource createDataSource(@NonNull String user, @NonNull String databaseName) throws SQLException
      Creates a DataSource with a specific user and database name.

      Creating the DataSource does not create the database or the user itself. This must be done by the calling code (e.g. with a EmbeddedPostgresPreparer).

      Throws:
      SQLException
    • getPort

      public int getPort()
      Returns the network (TCP) port for the PostgreSQL server instance.
    • instanceId

      @NonNull public String instanceId()
      Returns the instance id for the PostgreSQL server instance. This id is an alphanumeric string that can be used to differentiate between multiple embedded PostgreSQL server instances.
    • getPostgresVersion

      public String getPostgresVersion() throws IOException
      Return the version of the PostgreSQL installation that is used by this instance.
      Returns:
      A string representing the Postgres version as described in the Postgres versioning policy.
      Throws:
      IOException
      Since:
      4.1
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • close

      public void close() throws IOException
      Closing an EmbeddedPostgres instance shuts down the connected database instance.
      Specified by:
      close in interface AutoCloseable
      Throws:
      IOException