Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • PostgreSQL - Get Started
  • Install PostgreSQL
  • Connect to PostgreSQL DB
  • Create Database
  • Create Table
  • Copy Table
  • Drop Table
  • Drop Database
  • Truncate Table
  • ALTER Table
  • Rename Table
  • Rename Columns
  • Add Columns
  • Modify Column Type
  • Set Default Value of Column
  • Remove Columns
  • Add Constraints to Table
  • Insert Data
  • Upsert Data
  • Update Data
  • Delete Data
  • SELECT Statement
  • WHERE Clause
  • GROUP BY Clause
  • HAVING Clause
  • ORDER BY Clause
  • DISTINCT Clause
  • Inner Join
  • Left Outer Join
  • Right Outer Join
  • Full Outer Join
  • Self Join
  • Natural Join
  • Cross Join
  • LIMIT OFFSET Clause
  • GROUPING SETS
  • GROUPING() Function
  • GROUP BY CUBE
  • GROUP BY ROLLUP
  • Sub Query
  • ALL Operator
  • ANY Operator
  • UNION Operator
  • INTERSECT Operator
  • EXCEPT Operator
  • IS NULL Operator
  • BETWEEN Operator
  • LIKE Operator
  • CAST Operator
  • CASE Expressions
  • NULLIF()
  • COALESCE()
  • GREATEST(), LEAST()
  • WITH Queries (CTE)
  • Constraints
  • NOT NULL Constraint
  • Unique Constraint
  • Check Constraint
  • Primary Key
  • Foreign Key
  • Sequence
  • Serial Type
  • Identity Columns
  • Generated Columns
  • Data Types
  • Boolean Type
  • Character Type
  • Integer Type
  • Numeric Type
  • Date Type
  • Time Type
  • TimeStamp Type
  • Interval Type
  • Array Type
  • Json Type
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

PostgreSQL: TimeStamp Data Type

PostgreSQL TIMESTAMP datatype allows you to store both DATE and TIME values. However, it does not store time zone data.

In PostgreSQL, the TIMESTAMP datatype is classified into two temporal data types

  • TIMESTAMP: Datatype used to store time without a time zone.
  • TIMESTAMPZ: Datatype used to store time with a time zone.

Storage of data in TIMESTAMPZ:

  • In PostgreSQL TIMESTAMPZ store values in Universal Time Coordinated (UTC) format.
  • When you insert data into TIMESTAMPZ column, PostgreSQL convert the inserted value into UTC format and store it in the column.
  • When you query the TIMESTAMPZ column from a database, PostgreSQL converts the UTC value to the time value of the timezone set for a database server, the user, or the current database connection.

Now let's create timestamp_sample table containing timezone and timezonez datatypes to have a better understanding that how PostgreSQL handles them.

Example: Table with TimeStamp
CREATE TABLE timestamp_sample (
    ts 	 TIMESTAMP, 
    tsz TIMESTAMPTZ
);

We can check the current timezone of the database using the following:

Example: Get Timezone
SHOW TIMEZONE;
Or 
SHOW TIME ZONE;

As you can see, the current time zone is set to Asia/Calcutta. Now let's insert data into timestamp_sample table and select the data.

Example: Insert Time Zone Data
INSERT INTO timestamp_sample (ts, tsz)
VALUES('2022-05-20 15:00:10-09','2022-05-20 15:00:10-09');

SELECT * FROM timestamp_sample;

As you can see, the timezone column has the same value as inserted, but timezonez has a value as per the current time zone defined. You can override the time zone to America/New_York, that is EST for this database connection using the following command

Example: Set Time Zone
SET timezone = 'America/New_York';

Once the above command is executed, you can check the current timezone using the SHOW TIMEZONE command.

After that, run the SELECT query again to see data of timestamp_sample table.

Above, the timestampz column value changed again as per the new timestamp. It is always good practice to use timestampz datatype to store timestamp data.

PostgreSQL Timestamp functions

PostgreSQL provides some of the timestamp functions as below.

Get Current Timestamp with Time Zone

To get the current time value, PostgreSQL supports the now() function or CURRENT_TIMESTAMP function. Both of them shows timestamp with the time zone.

Example:
SELECT NOW(), CURRENT_TIMESTAMP;

Get Current Time with Time Zone

There is a function CURRENT_TIME that returns time with the time zone.

Example:
SELECT CURRENT_TIME;

Get Current Timestamp with Time Zone as String

Use the timeofday() function to get a timestamp with the time zone as string.

Convert between Time Zones

To convert a timestamp to another time zone, use the timezone(zone, timestamp) function. The timezone function takes two parameters, the zone is the time zone you want to convert and timestamp is the input timestamp value.

Let's check the current time zone first using SHOW TIMEZONE command.

Now, to convert current_timestamp to as per 'Asia/Calcutta' timezone, we can use the timezone() function, as shown below.

Example:
SELECT CURRENT_TIMESTAMP, timezone('Asia/Calcutta', CURRENT_TIMESTAMP);
TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.