Difference between timestamps with and without time zone in PostgreSQL
This article will discuss the timestamp types in PostgreSQL and show how they differ.
Timestamps in PostgreSQL
In PostgreSQL, there are two types of timestamps.
- Timestamp without time zone
- Timestamp with time zone
The first stores the local date. For example, suppose it is now 11.00 on a 24-hour system clock.
So this will be stored as 11.00. If it is saved in a database, say a remote database, and someone pulls this row from the CST time zone, he will still see it as 11.00.
However, this is not the real time. To view the time you need to convert to the CST time zone.
Since it does not store any information about the time zone, it cannot be further determined in different time zones.
Second approach solves this problem. So whenever you push a timestamp in database it will extract the timezone from host system and save the timezone along with the timestamp.
Assume we are in GMT+6 time zone. Here is a demonstration of timestamp.
SELECT now() as "System Time",
now()::timestamp as "postgres Time",
now() AT TIME ZONE 'GMT' as "time without zone",
now() AT TIME ZONE 'CST' as "time without zone",
now()::timestamp at TIME ZONE 'GMT' as "Timestamp GMT",
now()::timestamp at TIME ZONE 'CST' as "Timestamp CST" ;
Here, the first column contains the system time and the second column contains the timestamp after converting the time to a timestamp without time zone.
Output:
System Time | postgres Time | time without zone | time without zone | Timestamp GMT | Timestamp CST
-------------------------------+----------------------------+----------------------------+----------------------------+-------------------------------+-------------------------------
2022-03-15 10:19:05.432758+06 | 2022-03-15 10:19:05.432758 | 2022-03-15 04:19:05.432758 | 2022-03-14 22:19:05.432758 | 2022-03-15 16:19:05.432758+06 | 2022-03-15 22:19:05.432758+06
(1 row)
Difference between timestamps with and without time zone in PostgreSQL
无时区
Let's create a table and see how it can store timestamps and how to use them
in PostgreSQL 有时区
.
First, connect your psql console to Postgres and run the following SQL commands to create the following tables:
CREATE TABLE Times(
id INT PRIMARY KEY NOT NULL,
time_without_zone TIMESTAMP WITHOUT TIME ZONE DEFAULT now(),
time_with_zone TIMESTAMP WITH TIME ZONE DEFAULT now()
);
Now, populate the table with some entries.
INSERT INTO Times(id) VALUES(1);
INSERT INTO Times(id) VALUES(2);
INSERT INTO Times(id) VALUES(3);
INSERT INTO Times(id) VALUES(4);
INSERT INTO Times(id) VALUES(5);
INSERT INTO Times(id) VALUES(6);
INSERT INTO Times(id) VALUES(7);
Output:
postgres=# select * from times;
id | time_without_zone | time_with_zone
----+----------------------------+-------------------------------
1 | 2022-03-15 10:29:03.52078 | 2022-03-15 10:29:03.52078+06
2 | 2022-03-15 10:29:03.52564 | 2022-03-15 10:29:03.52564+06
3 | 2022-03-15 10:29:03.526723 | 2022-03-15 10:29:03.526723+06
4 | 2022-03-15 10:29:03.527775 | 2022-03-15 10:29:03.527775+06
5 | 2022-03-15 10:29:03.528865 | 2022-03-15 10:29:03.528865+06
6 | 2022-03-15 10:29:03.529941 | 2022-03-15 10:29:03.529941+06
7 | 2022-03-15 10:29:05.045774 | 2022-03-15 10:29:05.045774+06
(7 rows)
postgres=#
As you can see, time_with_zone
the column stores the time with the GMT+06 time zone. The time can be converted to any other time zone because psql will know the cardinality of the time in the column.
Here is a sample output that shows what happens if you try to insert a timestamp with a time zone on a column that does not have a time zone type.
INSERT INTO Times(id,time_without_zone,time_with_zone) VALUES(9,'2022-03-15 10:29:05.045774+06','2022-03-15 10:29:05.045774+06');
Output:
postgres=# select * from times where id=9;
id | time_without_zone | time_with_zone
----+----------------------------+-------------------------------
9 | 2022-03-15 10:29:05.045774 | 2022-03-15 10:29:05.045774+06
(1 row)
As you can see, psql simply time_without_zone
dropped the +06 in the column.
If you want to see all the time zones available in Postgres, you can run the following SQL command:
postgres=# SELECT name FROM pg_timezone_names;
name
----------------------------------
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
Africa/Asmara
Africa/Asmera
Africa/Bamako
Africa/Bangui
Africa/Banjul
Africa/Bissau
-- More --
Here, the system is running in the GMT+6 time zone. If you want to change the Postgres time zone to another time zone, you can run the following command:
postgres=# SET TIMEZONE='UTC';
SET
If you see the data in the table above, you will see that the column with the time zone has been successfully converted to UTC.
postgres=# select * from times;
id | time_without_zone | time_with_zone
----+----------------------------+-------------------------------
1 | 2022-03-15 10:29:03.52078 | 2022-03-15 04:29:03.52078+00
2 | 2022-03-15 10:29:03.52564 | 2022-03-15 04:29:03.52564+00
3 | 2022-03-15 10:29:03.526723 | 2022-03-15 04:29:03.526723+00
4 | 2022-03-15 10:29:03.527775 | 2022-03-15 04:29:03.527775+00
5 | 2022-03-15 10:29:03.528865 | 2022-03-15 04:29:03.528865+00
6 | 2022-03-15 10:29:03.529941 | 2022-03-15 04:29:03.529941+00
7 | 2022-03-15 10:29:05.045774 | 2022-03-15 04:29:05.045774+00
9 | 2022-03-15 10:29:05.045774 | 2022-03-15 04:29:05.045774+00
(8 rows)
You can see time_without_zone
that remains the same, but time_with_zone
converted from GMT+06 to UTC. To learn more about timestamp formats and representations, visit here .
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Terminate the PostgreSQL connection
Publish Date:2025/04/11 Views:200 Category:PostgreSQL
-
In this article, we will learn how to terminate a PostgreSQL session. Any open connections are run by background processes or tasks, PSQL which may no longer exist despite exiting the user interface or command line tool. Use ps -ef or grep
Single query to rename and change column type in PostgreSQL
Publish Date:2025/04/11 Views:166 Category:PostgreSQL
-
This article describes how to rename a column and change its type in PostgreSQL using only a single query. Renaming and changing column types in MySQL In MySQL , if you want to change the column type and rename it, you can use a simple stat
Joining columns using Select in PostgreSQL
Publish Date:2025/04/11 Views:177 Category:PostgreSQL
-
MySQL PostgreSQL is an object-relational database system, which means it can support more complex data types than its competitors . Today we will learn how to use SELECT the operator to join the columns of a table. Using operators to || joi
Using CASE in PostgreSQL
Publish Date:2025/04/11 Views:124 Category:PostgreSQL
-
This article shows how to use the statement in PostgreSQL CASE . CASE How to use the statement in PostgreSQL case Statements are similar to those in general-purpose programming languages if-else . But in SQL, if you want to write IF-ELSE ,
Using NOT IN with subqueries in PostgreSQL
Publish Date:2025/04/11 Views:93 Category:PostgreSQL
-
NOT IN The inverts the result of NOT simply using IN the operator. NOT IN The right side of the operator must have a subquery in which multiple columns are returned to check whether the expression matches the data. NOT IN Tends to return tr
Using variables in PostgreSQL
Publish Date:2025/04/11 Views:171 Category:PostgreSQL
-
This article will demonstrate how we can declare and assign values to variables in PostgreSQL. In PostgreSQL, DECLARE variables are declared using Often you will need variables in your PL/SQL scripts. In DECLARE the section called , y
Connect to PostgreSQL using a password
Publish Date:2025/04/11 Views:171 Category:PostgreSQL
-
This article shows various ways to connect to PostgreSQL using a password. It can be through the command line, pgpass a file, PGPASSWORD an environment variable or a connection string. Connecting to PostgreSQL with a password using the comm
Deleting a database in PostgreSQL via PSQL
Publish Date:2025/04/11 Views:166 Category:PostgreSQL
-
There are two ways to access PostgreSQL objects and databases on your system. One is through an interface, such as a graphical interface like PGADMIN, and the other is the basic command line tool psql. Today, we will look at DROP DATABASE t
Using the database in PostgreSQL
Publish Date:2025/04/11 Views:132 Category:PostgreSQL
-
This article demonstrates connecting to a database, creating a new database, and creating a table in PostgreSQL. Available databases in PostgreSQL You can run the following command after opening the Postgres command line to view all availab