如何修改dbt时区以匹配本地服务器时区(BigQuery连接)
I have dbt installed and running on a local Linux server connected to BigQuery, with this profiles.yml configuration:
www: outputs: dev: dataset: dw job_execution_timeout_seconds: 300 job_retries: 1 keyfile: /mykey.json location: asia-east2 method: service-account priority: interactive project: myprojectid threads: 4 type: bigquery target: dev
When I run dbt run, the event timestamps in the logs are in UTC, which doesn't match my server's timezone (HKT/UTC+8):
$ dbt run 04:18:08 Running with dbt=1.3.2 04:18:09 Unable to do partial parsing because a project config has changed 04:18:10 [WARNING]: Configuration paths exist in your dbt_project.yml file which do not apply to any resources. There are 1 unused configuration paths: - models.www.example 04:18:10 Found 1 model, 0 tests, 0 snapshots, 0 analyses, 319 macros, 0 operations, 0 seed files, 0 sources, 0 exposures, 0 metrics 04:18:10 04:18:11 Concurrency: 4 threads (target='dev') 04:18:11 04:18:11 1 of 1 START sql table model dw.dim_wine_color ................................. [RUN] 04:18:15 1 of 1 OK created sql table model dw.dim_wine_color ............................ [CREATE TABLE (195.1k rows, 6.5 MB processed) in 4.08s] 04:18:15 04:18:15 Finished running 1 table model in 0 hours 0 minutes and 5.14 seconds (5.14s). 04:18:15 04:18:15 Completed successfully 04:18:15 04:18:15 Done. PASS=1 WARN=0 ERROR=0 SKIP=0 TOTAL=1
My server's timezone is HKT (UTC+8):
$ date Thu 12 Jan 2023 12:18:20 PM HKT
Is there a configuration I can modify to make dbt's timestamps match the system timezone?
Absolutely! You can get dbt's log timestamps to match your server's HKT (UTC+8) timezone with either of these straightforward methods:
1. Quick Temporary Fix: Use the TZ Environment Variable
If you just want to test this for a single run, prefix your dbt command with the TZ variable set to your timezone:
TZ=Asia/Hong_Kong dbt run
This will immediately make dbt output logs using HKT timestamps, matching what you see from the date command on your server.
2. Permanent Fix: Add Timezone Config to dbt_project.yml
For a long-term solution, you can configure dbt to use your system timezone by default. Since you're running dbt 1.3.2 (which is part of dbt Core 1.2+), you can use the log-format-timezone setting in your project configuration.
Add these lines to your dbt_project.yml file:
# In dbt_project.yml log-format: "%(asctime)s %(levelname)s: %(message)s" log-format-timezone: "Asia/Hong_Kong"
The log-format line preserves the default log structure (feel free to tweak this if you prefer a different format), while log-format-timezone explicitly sets the timezone to HKT.
Once you save this change, every future dbt run (or other dbt commands) will automatically show timestamps in your server's local timezone.
A quick note: dbt uses UTC by default for logging to keep consistency across different environments, but these settings let you override that to match your local setup.
内容的提问来源于stack exchange,提问作者Kevin Lee

