Class EmbeddedPostgres
java.lang.Object
de.softwareforge.testing.postgres.embedded.EmbeddedPostgres
- All Implemented Interfaces:
AutoCloseable
Manages an embedded PostgreSQL server instance.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classCreates a newEmbeddedPostgresinstance and starts it. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe version of postgres used if no specific version has been given. -
Method Summary
Modifier and TypeMethodDescriptionstatic EmbeddedPostgres.Builderbuilder()Returns a newEmbeddedPostgres.Builder.static EmbeddedPostgres.BuilderReturns a builder with defaultEmbeddedPostgres.Builder.withDefaults()configuration already applied.voidclose()Closing anEmbeddedPostgresinstance shuts down the connected database instance.createDataSource(String user, String databaseName) Creates aDataSourcewith a specific user and database name.Creates aDataSourceobject that connects to the default database.Creates aDataSourceobject that connects to the standard system database.static EmbeddedPostgresReturns an instance that has been started and configured.booleanstatic EmbeddedPostgresThis returns anEmbeddedPostgresinstance that can be solely used for version checking.intgetPort()Returns the network (TCP) port for the PostgreSQL server instance.Return the version of the PostgreSQL installation that is used by this instance.inthashCode()Returns the instance id for the PostgreSQL server instance.toString()
-
Field Details
-
DEFAULT_POSTGRES_VERSION
The version of postgres used if no specific version has been given.- See Also:
-
-
Method Details
-
defaultInstance
Returns an instance that has been started and configured. TheEmbeddedPostgres.Builder.withDefaults()configuration has been applied.- Throws:
IOException
-
builderWithDefaults
Returns a builder with defaultEmbeddedPostgres.Builder.withDefaults()configuration already applied. -
forVersionCheck
This returns anEmbeddedPostgresinstance that can be solely used for version checking. It has not been booted and will not work for any other things but executinggetPostgresVersion(). 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
EmbeddedPostgresinstance. - Throws:
IOException- Could not create the instance.- Since:
- 4.1
-
builder
Returns a newEmbeddedPostgres.Builder. -
createTemplateDataSource
Creates aDataSourceobject that connects to the standard system database.The standard system database is the
template1database.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
Creates aDataSourceobject that connects to the default database.The default database is the
postgresdatabase.- Throws:
SQLException
-
createDataSource
@Nonnull public DataSource createDataSource(@Nonnull String user, @Nonnull String databaseName) throws SQLException Creates aDataSourcewith 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
Returns the network (TCP) port for the PostgreSQL server instance. -
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
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
-
equals
-
hashCode
-
close
Closing anEmbeddedPostgresinstance shuts down the connected database instance.- Specified by:
closein interfaceAutoCloseable- Throws:
IOException
-