Scalingo App TZ Variable and Managed PostgreSQL Session Timezone Interoperability
0 reputation · 22 Nov 2024, 13:51 UTC
0 reputation · 22 Nov 2024, 13:51 UTC
Applications running on Scalingo can define a TZ environment variable that is injected into the container and influences the runtime clock used by language‑specific APIs. Managed PostgreSQL addons on the platform, however, default to UTC for session time zone unless the application explicitly issues a SET TIME ZONE command after connecting. This creates a potential mismatch between timestamps generated by the application code and those stored or returned by the database, especially when the app relies on the injected TZ for civil‑time calculations. The goal is to determine whether the platform automatically propagates the app‑level TZ variable to the PostgreSQL session, or whether developers must handle the conversion themselves to achieve consistent timestamp handling across the app‑database boundary.
Does Scalingo automatically set the PostgreSQL session time zone to the value of the app‑level TZ environment variable for new connections?
If not, what is the recommended approach for the application to synchronize its civil‑time logic with the database without introducing drift?
How does the Scheduler’s default UTC execution affect jobs that depend on the same TZ‑aware timestamps?
33275 reputation · 22 Nov 2024, 14:27 UTC
No, Scalingo does not automatically propagate the application-level TZ environment variable to your managed PostgreSQL session. The TZ variable modifies the container's runtime environment and the behavior of language-specific time APIs (e.g., Node.js, Python, Ruby), but it has no effect on the external PostgreSQL instance, which defaults to UTC for all new sessions.
To ensure consistency between your application's civil-time logic and the database, you should avoid relying on the database's session timezone for business logic. Instead, follow these implementation standards:
TIMESTAMPTZ: Define your columns as TIMESTAMP WITH TIME ZONE. PostgreSQL stores these values in UTC internally and converts them to the session timezone upon retrieval.CURRENT_TIMESTAMP) to match the app's TZ, you must issue a SET TIME ZONE command immediately after establishing a connection.Rather than calling SET TIME ZONE on every query, configure your connection pool's initialization logic to run the command once per connection:
-- Example SQL command to run on connection initialization
SET TIME ZONE 'Europe/Paris';
The Scalingo Scheduler executes jobs in UTC by default. This creates a disconnect if your job logic assumes the TZ variable is active or if the job triggers based on a local-time calculation. To prevent drift:
TZ variable within the job script to ensure the runtime clock is adjusted before querying the database.To verify the current mismatch, run the following commands in your environment:
SHOW TIMEZONE; via a database client to confirm the managed default (likely UTC).SELECT now(); from the database against the current time returned by your application's native time API.Diagnostic Note: Please specify if you are using a connection pooler (like PgBouncer) in transaction mode, as this prevents the use of SET TIME ZONE for session persistence.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.