Class EmbeddedPostgres
java.lang.Object
de.softwareforge.testing.postgres.embedded.EmbeddedPostgres
- All Implemented Interfaces:
AutoCloseable
Manages an embedded PostgreSQL server instance.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Creates a newEmbeddedPostgres
instance and starts it. -
Field Summary
Modifier and TypeFieldDescriptionstatic final String
The version of postgres used if no specific version has been given. -
Method Summary
Modifier and TypeMethodDescriptionstatic EmbeddedPostgres.Builder
builder()
Returns a newEmbeddedPostgres.Builder
.static EmbeddedPostgres.Builder
Returns a builder with defaultEmbeddedPostgres.Builder.withDefaults()
configuration already applied.void
close()
Closing anEmbeddedPostgres
instance shuts down the connected database instance.createDataSource
(String user, String databaseName) Creates aDataSource
with a specific user and database name.Creates aDataSource
object that connects to the default database.Creates aDataSource
object that connects to the standard system database.static EmbeddedPostgres
Returns an instance that has been started and configured.boolean
static EmbeddedPostgres
This returns anEmbeddedPostgres
instance that can be solely used for version checking.int
getPort()
Returns the network (TCP) port for the PostgreSQL server instance.Return the version of the PostgreSQL installation that is used by this instance.int
hashCode()
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 anEmbeddedPostgres
instance 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
EmbeddedPostgres
instance. - Throws:
IOException
- Could not create the instance.- Since:
- 4.1
-
builder
Returns a newEmbeddedPostgres.Builder
. -
createTemplateDataSource
Creates aDataSource
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
Creates aDataSource
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 aDataSource
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
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 anEmbeddedPostgres
instance shuts down the connected database instance.- Specified by:
close
in interfaceAutoCloseable
- Throws:
IOException
-