Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/modules/databases/postgres.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Testcontainers module for [PostgresSQL](https://hub.docker.com/_/postgres)
You can start a PostgreSQL container instance from any Java application by using:

<!--codeinclude-->
[Container creation](../../../modules/postgresql/src/test/java/org/testcontainers/junit/postgresql/SimplePostgreSQLTest.java) inside_block:container
[Container creation](../../../modules/postgresql/src/test/java/org/testcontainers/postgresql/PostgreSQLContainerTest.java) inside_block:container
<!--/codeinclude-->

See [Database containers](./index.md) for documentation and usage that is common to all relational database container types.
Expand All @@ -28,19 +28,19 @@ See [JDBC](./jdbc.md) for documentation.
* [pgvector/pgvector](https://hub.docker.com/r/pgvector/pgvector)

<!--codeinclude-->
[Using pgvector](../../../modules/postgresql/src/test/java/org/testcontainers/containers/CompatibleImageTest.java) inside_block:pgvectorContainer
[Using pgvector](../../../modules/postgresql/src/test/java/org/testcontainers/postgresql/CompatibleImageTest.java) inside_block:pgvectorContainer
<!--/codeinclude-->

* [postgis/postgis](https://registry.hub.docker.com/r/postgis/postgis)

<!--codeinclude-->
[Using PostGIS](../../../modules/postgresql/src/test/java/org/testcontainers/containers/CompatibleImageTest.java) inside_block:postgisContainer
[Using PostGIS](../../../modules/postgresql/src/test/java/org/testcontainers/postgresql/CompatibleImageTest.java) inside_block:postgisContainer
<!--/codeinclude-->

* [timescale/timescaledb](https://hub.docker.com/r/timescale/timescaledb)

<!--codeinclude-->
[Using TimescaleDB](../../../modules/postgresql/src/test/java/org/testcontainers/containers/CompatibleImageTest.java) inside_block:timescaledbContainer
[Using TimescaleDB](../../../modules/postgresql/src/test/java/org/testcontainers/postgresql/CompatibleImageTest.java) inside_block:timescaledbContainer
<!--/codeinclude-->

## Adding this module to your project dependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
* Supported images: {@code postgres}, {@code pgvector/pgvector}
* <p>
* Exposed ports: 5432
*
* @deprecated use {@link org.testcontainers.postgresql.PostgreSQLContainer} instead.
*/
@Deprecated
public class PostgreSQLContainer<SELF extends PostgreSQLContainer<SELF>> extends JdbcDatabaseContainer<SELF> {

public static final String NAME = "postgresql";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package org.testcontainers.postgresql;

import org.jetbrains.annotations.NotNull;
import org.testcontainers.containers.JdbcDatabaseContainer;
import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy;
import org.testcontainers.utility.DockerImageName;

import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.Set;

/**
* Testcontainers implementation for PostgreSQL.
* <p>
* Supported images: {@code postgres}, {@code pgvector/pgvector}
* <p>
* Exposed ports: 5432
*/
public class PostgreSQLContainer extends JdbcDatabaseContainer<PostgreSQLContainer> {

public static final String NAME = "postgresql";

public static final String IMAGE = "postgres";

public static final String DEFAULT_TAG = "9.6.12";

private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("postgres");

private static final DockerImageName PGVECTOR_IMAGE_NAME = DockerImageName.parse("pgvector/pgvector");

public static final Integer POSTGRESQL_PORT = 5432;

static final String DEFAULT_USER = "test";

static final String DEFAULT_PASSWORD = "test";

private String databaseName = "test";

private String username = "test";

private String password = "test";

private static final String FSYNC_OFF_OPTION = "fsync=off";

public PostgreSQLContainer(final String dockerImageName) {
this(DockerImageName.parse(dockerImageName));
}

public PostgreSQLContainer(final DockerImageName dockerImageName) {
super(dockerImageName);
dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME, PGVECTOR_IMAGE_NAME);

this.waitStrategy =
new LogMessageWaitStrategy()
.withRegEx(".*database system is ready to accept connections.*\\s")
.withTimes(2)
.withStartupTimeout(Duration.of(60, ChronoUnit.SECONDS));
this.setCommand("postgres", "-c", FSYNC_OFF_OPTION);

addExposedPort(POSTGRESQL_PORT);
}

/**
* @return the ports on which to check if the container is ready
* @deprecated use {@link #getLivenessCheckPortNumbers()} instead
*/
@NotNull
@Override
@Deprecated
protected Set<Integer> getLivenessCheckPorts() {
return super.getLivenessCheckPorts();
}

@Override
protected void configure() {
// Disable Postgres driver use of java.util.logging to reduce noise at startup time
withUrlParam("loggerLevel", "OFF");
addEnv("POSTGRES_DB", databaseName);
addEnv("POSTGRES_USER", username);
addEnv("POSTGRES_PASSWORD", password);
}

@Override
public String getDriverClassName() {
return "org.postgresql.Driver";
}

@Override
public String getJdbcUrl() {
String additionalUrlParams = constructUrlParameters("?", "&");
return (
"jdbc:postgresql://" +
getHost() +
":" +
getMappedPort(POSTGRESQL_PORT) +
"/" +
databaseName +
additionalUrlParams
);
}

@Override
public String getDatabaseName() {
return databaseName;
}

@Override
public String getUsername() {
return username;
}

@Override
public String getPassword() {
return password;
}

@Override
public String getTestQueryString() {
return "SELECT 1";
}

@Override
public PostgreSQLContainer withDatabaseName(final String databaseName) {
this.databaseName = databaseName;
return self();
}

@Override
public PostgreSQLContainer withUsername(final String username) {
this.username = username;
return self();
}

@Override
public PostgreSQLContainer withPassword(final String password) {
this.password = password;
return self();
}

@Override
protected void waitUntilContainerStarted() {
getWaitStrategy().waitUntilReady(this);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.testcontainers.containers;
package org.testcontainers.postgresql;

import org.junit.jupiter.api.Test;
import org.testcontainers.db.AbstractContainerDatabaseTest;
Expand All @@ -15,7 +15,7 @@ class CompatibleImageTest extends AbstractContainerDatabaseTest {
void pgvector() throws SQLException {
try (
// pgvectorContainer {
PostgreSQLContainer<?> pgvector = new PostgreSQLContainer<>("pgvector/pgvector:pg16")
PostgreSQLContainer pgvector = new PostgreSQLContainer("pgvector/pgvector:pg16")
// }
) {
pgvector.start();
Expand All @@ -30,7 +30,7 @@ void pgvector() throws SQLException {
void postgis() throws SQLException {
try (
// postgisContainer {
PostgreSQLContainer<?> postgis = new PostgreSQLContainer<>(
PostgreSQLContainer postgis = new PostgreSQLContainer(
DockerImageName.parse("postgis/postgis:16-3.4-alpine").asCompatibleSubstituteFor("postgres")
)
// }
Expand All @@ -47,7 +47,7 @@ void postgis() throws SQLException {
void timescaledb() throws SQLException {
try (
// timescaledbContainer {
PostgreSQLContainer<?> timescaledb = new PostgreSQLContainer<>(
PostgreSQLContainer timescaledb = new PostgreSQLContainer(
DockerImageName.parse("timescale/timescaledb:2.14.2-pg16").asCompatibleSubstituteFor("postgres")
)
// }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package org.testcontainers.junit.postgresql;
package org.testcontainers.postgresql;

import org.junit.jupiter.api.Test;
import org.testcontainers.PostgreSQLTestImages;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.db.AbstractContainerDatabaseTest;

import java.sql.ResultSet;
Expand All @@ -13,7 +12,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatNoException;

class SimplePostgreSQLTest extends AbstractContainerDatabaseTest {
class PostgreSQLContainerTest extends AbstractContainerDatabaseTest {
static {
// Postgres JDBC driver uses JUL; disable it to avoid annoying, irrelevant, stderr logs during connection testing
LogManager.getLogManager().getLogger("").setLevel(Level.OFF);
Expand All @@ -22,7 +21,7 @@ class SimplePostgreSQLTest extends AbstractContainerDatabaseTest {
@Test
void testSimple() throws SQLException {
try ( // container {
PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:9.6.12")
PostgreSQLContainer postgres = new PostgreSQLContainer("postgres:9.6.12")
// }
) {
postgres.start();
Expand All @@ -37,7 +36,7 @@ void testSimple() throws SQLException {
@Test
void testCommandOverride() throws SQLException {
try (
PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>(PostgreSQLTestImages.POSTGRES_TEST_IMAGE)
PostgreSQLContainer postgres = new PostgreSQLContainer(PostgreSQLTestImages.POSTGRES_TEST_IMAGE)
.withCommand("postgres -c max_connections=42")
) {
postgres.start();
Expand All @@ -51,7 +50,7 @@ void testCommandOverride() throws SQLException {
@Test
void testUnsetCommand() throws SQLException {
try (
PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>(PostgreSQLTestImages.POSTGRES_TEST_IMAGE)
PostgreSQLContainer postgres = new PostgreSQLContainer(PostgreSQLTestImages.POSTGRES_TEST_IMAGE)
.withCommand("postgres -c max_connections=42")
.withCommand()
) {
Expand All @@ -66,7 +65,7 @@ void testUnsetCommand() throws SQLException {
@Test
void testMissingInitScript() {
try (
PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>(PostgreSQLTestImages.POSTGRES_TEST_IMAGE)
PostgreSQLContainer postgres = new PostgreSQLContainer(PostgreSQLTestImages.POSTGRES_TEST_IMAGE)
.withInitScript(null)
) {
assertThatNoException().isThrownBy(postgres::start);
Expand All @@ -76,7 +75,7 @@ void testMissingInitScript() {
@Test
void testExplicitInitScript() throws SQLException {
try (
PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>(PostgreSQLTestImages.POSTGRES_TEST_IMAGE)
PostgreSQLContainer postgres = new PostgreSQLContainer(PostgreSQLTestImages.POSTGRES_TEST_IMAGE)
.withInitScript("somepath/init_postgresql.sql")
) {
postgres.start();
Expand All @@ -91,7 +90,7 @@ void testExplicitInitScript() throws SQLException {
@Test
void testExplicitInitScripts() throws SQLException {
try (
PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>(PostgreSQLTestImages.POSTGRES_TEST_IMAGE)
PostgreSQLContainer postgres = new PostgreSQLContainer(PostgreSQLTestImages.POSTGRES_TEST_IMAGE)
.withInitScripts("somepath/init_postgresql.sql", "somepath/init_postgresql_2.sql")
) {
postgres.start();
Expand All @@ -112,7 +111,7 @@ void testExplicitInitScripts() throws SQLException {
@Test
void testWithAdditionalUrlParamInJdbcUrl() {
try (
PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>(PostgreSQLTestImages.POSTGRES_TEST_IMAGE)
PostgreSQLContainer postgres = new PostgreSQLContainer(PostgreSQLTestImages.POSTGRES_TEST_IMAGE)
.withUrlParam("charSet", "UNICODE")
) {
postgres.start();
Expand All @@ -123,7 +122,7 @@ void testWithAdditionalUrlParamInJdbcUrl() {
}
}

private void assertHasCorrectExposedAndLivenessCheckPorts(PostgreSQLContainer<?> postgres) {
private void assertHasCorrectExposedAndLivenessCheckPorts(PostgreSQLContainer postgres) {
assertThat(postgres.getExposedPorts()).containsExactly(PostgreSQLContainer.POSTGRESQL_PORT);
assertThat(postgres.getLivenessCheckPortNumbers())
.containsExactly(postgres.getMappedPort(PostgreSQLContainer.POSTGRESQL_PORT));
Expand Down
Loading