EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PostgreSQL Tutorial PostgreSQL TIME
 

PostgreSQL TIME

Sohel Sayyad
Article bySohel Sayyad
EDUCBA
Reviewed byRavi Rathore

Updated May 15, 2023

PostgreSQL TIME

 

 

Introduction to PostgreSQL TIME

PostgreSQL provides various data types to handle the data. The TIME data type will allow the user to store the values like the time of the day. The time range for the day starts from 00:00:00 to 24:00:00. We can store the time of day in various formats like HH: MM, HH:MM:: SS, and HHMMSS, etc. Also, we can use precision as well by using formats like MM: SS.pppppp, HH:MM: SS.pppppp, and HHMMSS.pppppp, etc., which can be up to six digits. The PostgreSQL TIME data type requires 8 bytes to store the time values.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax:

Given below is the following syntax to illustrate the declaration of a column having as data type as the TIME data type:

column TIME(precision);

Explanation:

  • column: The column name which will have a data type as TIME.
  • precision: This is a fractional digits number placed in the seconds’ field. This can be up to six digits.

How does PostgreSQL TIME Data Type work?

Including the ISO 8601 and the SQL-compatibility, the PostgreSQL also allows all TIME formats.

Consider the following TIME formats allowed in PostgreSQL.

  • HH:MM
  • HH:MM:SS
  • HHMMSS

Now, let us see the following values as per the above TIME formats.

  • 03:04
  • 03:04:05
  • 030405

We can include the precision value in the TIME value using the following TIME formats.

  • MM: SS.pppppp
  • HH:MM:SS.pppppp
  • pppppp

The character p is used for defining precision.

Now, let us see the following values as per the above TIME formats.

  • 02:36.666666
  • 03:04:05.444444
  • 030506.999999

Examples of PostgreSQL TIME

Given below are the examples mentioned:

Example #1

We generally store the time of the day with the help of the data type named TIME, like the tuition or event.

Here we will create a new table of name ‘tuitions’ using the CREATE TABLE statement, which will store the details of the ‘tuitions’ information.

Code:

CREATE TABLE tuitions (
id serial PRIMARY KEY,
tuition_name VARCHAR NOT NULL,
tuition_start_tm TIME NOT NULL,
tuition_end_tm TIME NOT NULL
);

We will insert some data into the tuitions table using the INSERT INTO statement as follows.

Code:

INSERT INTO tuitions(tuition_name, tuition_start_tm, tuition_end_tm)
VALUES
('Maths', '07:00:00', '08:45:00'),
('English', '09:00:00', '11:45:00'),
('Algebra', '12:15:00', '14:00:00'),
('CS', '14:15:00', '17:00:00'),
('History', '17:15:00', '19:00:00');

Illustrate the result of the tuitions table by using the following SQL statement and a snapshot.

Code:

SELECT * FROM tuitions;

Output:

PostgreSQL TIME 1

Example #2

Consider the following examples of Handling PostgreSQL TIME values.

a. Get the current time

We can use the CURRENT_TIME function to get the current time with the time zone as follows.

Code:

SELECT CURRENT_TIME;

Output:

PostgreSQL TIME 2

b. We can use the CURRENT_TIME (precision) function to get the current time’s value containing the particular precision.

Code:

SELECT CURRENT_TIME(6);

Output:

PostgreSQL TIME 3

If we have not defined the precision, then we get the full precision as a result of the CURRENT_TIME function.

Example #3

To get the local time, we can use the PostgreSQL LOCALTIME function.

Code:

SELECT LOCALTIME;

Output:

PostgreSQL TIME 4

Example #4

We can use the PostgreSQL LOCAL TIME (precision) function as defined below to get the local time’s value containing the particular precision.

Code:

SELECT localtime(2);

Output:

containing the particular precision

Example #5

We can extract fields like seconds, minutes, and hours from a value formatted as TIME using the PostgreSQL EXTRACT function.

Code:

EXTRACT(extract_field FROM time);

Explanation:

  • extract_field: This value can be the milliseconds, second, minute, hour, etc.

Code:

SELECT
CURRENT_TIME,
EXTRACT
( HOUR FROM CURRENT_TIME) as CURRENT_TIME_HOUR,
EXTRACT
( MINUTE FROM CURRENT_TIME) as CURRENT_TIME_MINUTE,
EXTRACT
( SECOND FROM CURRENT_TIME) as CURRENT_TIME_SECOND,
EXTRACT
( milliseconds FROM CURRENT_TIME) as CURRENT_TIME_MILLISECONDS;

Output:
fields like seconds, minutes and hours

Example #6

We can convert the time from one time zone to another time zone.

Syntax:

[time_zone TIME] AT TIME ZONE time_zone

Consider the following example, which will convert the CURRENT_TIME to the time zone GMT+2.

Code:

SELECT CURRENT_TIME AT TIME ZONE 'GMT+2';

Output:

PostgreSQL TIME 7

Example #7

We can perform some arithmetic operations like addition, subtraction, and multiplication on time values.

a. Consider the following example to get the interval.

Code:

SELECT
time '11:00' - time '03:00';

Output:

to get the interval

b. Consider another example where the current time incremented by 3 hours.

Code:

SELECT CURRENT_TIME + interval '3 hours';

Output:

current time incremented by 3 hours

Recommended Articles

We hope that this EDUCBA information on “PostgreSQL TIME” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. PostgreSQL GRANT
  2. PostgreSQL round
  3. FULL OUTER JOIN PostgreSQL
  4. PostgreSQL Inner Join

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW