Skip to content

[Bug]: Postgres from testcontainers does not respect environment variable TZ (time zone) #10382

@michaldo

Description

@michaldo

Module

PostgreSQL

Testcontainers version

1.21.3

Using the latest Testcontainers version?

Yes

Host OS

Windows

Host Arch

x86

Docker version

Client:
 Version:           28.2.2
 API version:       1.50
 Go version:        go1.24.3
 Git commit:        e6534b4
 Built:             Fri May 30 12:07:16 2025
 OS/Arch:           windows/amd64
 Context:           desktop-linux

Server: Docker Desktop 4.42.1 (196648)
 Engine:
  Version:          28.2.2
  API version:      1.50 (minimum version 1.24)
  Go version:       go1.24.3
  Git commit:       45873be
  Built:            Fri May 30 12:07:26 2025
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.7.27
  GitCommit:        05044ec0a9a75232cad458027ca83437aae3f4da
 runc:
  Version:          1.2.5
  GitCommit:        v1.2.5-0-g59923ef
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

What happened?

I expect I can setup server time zone. I tried all opportunities I found:

    public static void main(String[] args) throws SQLException {
        PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:16.3")
                .withEnv("TZ", "UTC")
                .withEnv("PGTZ", "UTC")
                .withCommand("postgres", "-c", "timezone=UTC");

        postgres.start();

        try (Connection conn = postgres.createConnection("")) {
            try (Statement stmt = conn.createStatement()) {
                ResultSet rs = stmt.executeQuery("SHOW TIME ZONE");
                if (rs.next()) {
                    System.out.println("Postgres server timezone: " + rs.getString(1));
                }
            }
        }
    }

Relevant log output

Postgres server timezone: Europe/Warsaw

Additional Information

If I run
docker run -it -e "TZ=Europe/Madrid" --rm -p 5432:5432 -e POSTGRES_PASSWORD=password postgres:16.3
SHOW TIME ZONE outputs "Europe/Madrid"

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions