How to Change a Database's Default Time Zone When the Database Time Is Different from the System Time
Symptom
The database time is inconsistent with the operating system time. After a user queries the default database time SYSDATE, it is found that the database time is eight hours later than the Beijing time. As a result, the updated data cannot be accurately located.
Possible Causes
The UTC time zone is used to display and interpret GaussDB(DWS) database timestamps. If the operating system's time zone is not the UTC, time of the GaussDB(DWS) database will be inconsistent with the system time. Generally, the time zone of the cluster does not need to be changed. Configuring the time zone of the client may affect SQL execution.
Prerequisites
You are advised to modify the timezone parameter during off-peak hours.
Handling Procedure
Method 1: Change the default time zone of the database in a GaussDB(DWS) cluster.
- Log in to the GaussDB(DWS) management console.
- In the navigation tree on the left, click Clusters.
- In the cluster list, find the target cluster and click its name. The Basic Information page is displayed.
- Click the Parameter Modifications tab and change the value of parameter timezone to your time zone. Then click Save.
- In the Modification Preview dialog box, confirm the modification and click Save.
- (Optional) Check the Restart Cluster column of the timezone parameter. It indicates whether you need to restart the cluster to make the parameter modification take effect.
The modification of the timezone parameter takes effect immediately. You do not need to restart the cluster.
Method 2: Run background commands to query and change the database time zone.
- Query the time zone and current time of the client. The time zone of the client is UTC, and the now() function returns the current time.
1 2 3 4 5 6 7 8 9 10 11
show time zone; TimeZone ---------- UTC (1 row) select now(); now ------------------------------- 2022-05-16 06:05:58.711454+00 (1 row)
- Create a data table. timestamp and timestamptz are common time types. timestamp does not store the time zone, and timestamptz does.
1 2 3 4 5 6 7 8 9
CREATE TABLE timezone_test (id int, t1 timestamp, t2 timestamptz) DISTRIBUTE BY HASH (id); \d timezone_test Table "public.timezone_test" Column | Type | Modifiers --------+-----------------------------+----------- id | integer | t1 | timestamp without time zone | t2 | timestamp with time zone |
- Insert the current time into the timezone_test table and query the current table.
1 2 3 4 5 6 7 8 9 10 11
insert into timezone_test values (1, now(), now() ); show time zone; TimeZone ---------- UTC (1 row) select * from timezone_test; id | t1 | t2 ----+----------------------------+------------------------------- 1 | 2022-05-16 06:10:04.564599 | 2022-05-16 06:10:04.564599+00 (1 row)
The t1 (timestamp type) parameter discards the time zone information when saving data. The t2 (timestamptz type) parameter saves the time zone information.
- Set the time zone of the client to UTC-8. Query the timezone_test table again.
1 2 3 4 5 6 7 8 9 10 11
set time zone 'UTC-8'; show time zone; TimeZone ---------- UTC-8 (1 row) select now(); now ------------------------------- 2022-05-16 14:13:43.175416+08 (1 row)
- Insert the current time to the timezone_test table and query the table. The value inserted to t1 is UTC-8 time, and t2 converts the time based on the time zone of the client.
1 2 3 4 5 6 7
insert into timezone_test values (2, now(), now() ); select * from timezone_test; id | t1 | t2 ----+----------------------------+------------------------------- 1 | 2022-05-16 06:10:04.564599 | 2022-05-16 14:10:04.564599+08 2 | 2022-05-16 14:15:03.715265 | 2022-05-16 14:15:03.715265+08 (2 rows)
- The timestamp type is affected the time zone used when data is inserted. The query result is not affected by the time zone of the client.
- The timestamptz type records the time zone information used when data is inserted. In a query, the time is converted based on the time zone of the client.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot