PostgreSQL Tutorial

  • Home
  • Stored Procedures
  • Triggers
  • Views
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
  • Functions
Home / PostgreSQL Tutorial / A Quick Guide To The PostgreSQL TIME Data Type

A Quick Guide To The PostgreSQL TIME Data Type

Summary: this tutorial introduces you to the PostgreSQL TIME data types and some handy functions to handle time values.

Introduction to PostgreSQL TIME data type

PostgreSQL provides the TIME data type that allows you to store and manipulate the time of day values.

The following statement illustrates how to declare a column with the TIME data type:

1
column_name TIME(precision);

A time value may have a precision with up to 6 digits. The precision specifies the number of factional digits placed in the second field.

The TIME data type requires 8 bytes storage size and its allowed range is from 00:00:00 to 24:00:00. The following illustrates the common formats of the TIME values:

1
2
3
HH:MM  
HH:MM:SS
HHMMSS

For example:

1
2
3
01:02
01:02:03
010203

If you want to use the precision, you can use the following formats:

1
2
3
HH:MM.pppppp    
HH:MM:SS.pppppp
HHMMSS.pppppp

In this form, p is the precision. For example:

1
2
3
04:59.999999
04:05:06.777777
040506.777777

PostgreSQL actually accepts almost any reasonable TIME format including SQL-compatible, ISO 8601, etc.

PostgreSQL TIME example

We often use the TIME data type for the columns that store time of day only e.g., the time of an event or a shift. Consider the following example.

First, create a new table named shifts by using the CREATE TABLE statement:

1
2
3
4
5
6
CREATE TABLE shifts (
    id serial PRIMARY KEY,
    shift_name VARCHAR NOT NULL,
    start_at TIME NOT NULL,
    end_at TIME NOT NULL
);  

Second, insert some rows into the shifts table:

1
2
3
4
INSERT INTO shifts(shift_name, start_at, end_at)
VALUES('Morning', '08:00:00', '12:00:00'),
      ('Afternoon', '13:00:00', '17:00:00'),
      ('Night', '18:00:00', '22:00:00');

Third, query data from the shifts table:

1
2
3
4
SELECT
    *
FROM
    shifts;

PostgreSQL TIME Data Type Example

PostgreSQL TIME with time zone type

Besides the TIME data type, PostgreSQL provides the TIME with time zone data type that allows you to store and manipulate time of day with time zone.

The following statement illustrates how to declare a column whose data type is TIME with time zone:

1
column TIME with time zone

The storage size of the TIME with time zone data type is 12 bytes that allow you store a time value with time zone in the range from 00:00:00+1459 to 24:00:00-1459.

The following illustrates the TIME with time zone values:

1
2
04:05:06 PST    
04:05:06.789-8  

Handling PostgreSQL TIME values

Getting the current time

To get the current time with time zone, you use the CURRENT_TIME function as follows:

1
2
3
4
5
6
SELECT CURRENT_TIME;
 
timetz
--------------------
00:51:02.746572-08
(1 row)

To get current time with a specific precision, you use the CURRENT_TIME(precision) function:

1
2
3
4
5
6
SELECT CURREN_TIME(5);
 
      timetz
-------------------
00:52:12.19515-08
(1 row)

Notice that without specifying the precision, the CURRENT_TIME function returns a time value with the full available precision.

To get the local time, you use the LOCALTIME function:

1
2
3
4
5
6
SELECT LOCALTIME;
 
      time
-----------------
00:52:40.227186
(1 row)

Similarly, to get the local time with a specific precision, you use the LOCALTIME(precision) function:

1
2
3
4
5
6
SELECT localtime(0);
 
   time
----------
00:56:08
(1 row)  

Converting time to a different timezone

To convert time to an different time zone, you use the following form:

1
[TIME with time zone] AT TIME ZONE time_zone

For example, to convert the local time to the time at the time zone UTC-7, you use the following statement:

1
2
3
4
5
6
SELECT LOCALTIME AT TIME ZONE 'UTC-7';
 
      timezone
--------------------
16:02:38.902271+07
(1 row)

Extracting hours, minutes, seconds from a time value

To extracting hours, minutes, seconds from a time value, you use the EXTRACT function as follows:

1
EXTRACT(field FROM time_value);

The field can be hour, minute, second, milliseconds as shown in the following example:

1
2
3
4
5
6
SELECT
    LOCALTIME,
    EXTRACT (HOUR FROM LOCALTIME) as hour,
    EXTRACT (MINUTE FROM LOCALTIME) as minute,
    EXTRACT (SECOND FROM LOCALTIME) as second,
    EXTRACT (milliseconds FROM LOCALTIME) as milliseconds;

PostgreSQL TIME example

Arithmetic operations on time values

PostgreSQL allows you to apply arithmetic operators such as +, -, * , etc.,  on time values and between time and interval values.

The following statement returns an interval between two time values:

1
2
3
4
5
6
SELECT time '10:00' - time '02:00';
 
?column?
----------
08:00:00
(1 row)

The following statement adds 2 hours to the local time:

1
2
3
4
5
6
SELECT LOCALTIME + interval '2 hours';
 
    ?column?
-----------------
03:16:18.020418
(1 row)

In this example, the sum of a time value and an interval value is a time value.

In this tutorial, you have learned about the PostgreSQL TIME data type and how to handle time values using time related functions.

Previous Tutorial: A Comprehensive Look at PostgreSQL Interval Data Type
Next Tutorial: The Basics Of PostgreSQL UUID Data Type

PostgreSQL Quick Start

  • What is PostgreSQL?
  • Install PostgreSQL
  • Connect to Database
  • Download PostgreSQL Sample Database
  • Load Sample Database
  • Explore Server and Database Objects

PostgreSQL Fundamentals

  • PostgreSQL Select
  • PostgreSQL Order By
  • PostgreSQL Select Distinct
  • PostgreSQL Where
  • PostgreSQL LIMIT
  • PostgreSQL IN
  • PostgreSQL Between
  • PostgreSQL Like
  • PostgreSQL Inner Join
  • PostgreSQL Left Join
  • PostgreSQL Full Outer Join
  • PostgreSQL Cross Join
  • PostgreSQL Natural Join
  • PostgreSQL Group By
  • PostgreSQL Having
  • PostgreSQL Union
  • PostgreSQL Intersect
  • PostgreSQL Except
  • PostgreSQL Subquery
  • PostgreSQL Insert
  • PostgreSQL Update
  • PostgreSQL Delete
  • PostgreSQL Data Types
  • PostgreSQL Create Table
  • PostgreSQL Alter Table
  • PostgreSQL Drop Table
  • PostgreSQL Truncate Table
  • PostgreSQL CHECK Constraint
  • PostgreSQL Not-Null Constraint
  • PostgreSQL Foreign Key
  • PostgreSQL Primary Key
  • PostgreSQL UNIQUE Constraint

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • PostgreSQL UPDATE Join with A Practical Example
  • PostgreSQL Cheat Sheet
  • PostgreSQL vs. MySQL
  • A Step-by-Step Guide To PostgreSQL Temporary Table
  • PostgreSQL RENAME COLUMN: Renaming One or More Columns of a Table
  • PostgreSQL Rename Table: A Step-by-Step Guide
  • PostgreSQL Change Column Type: Step-by-Step Examples
  • PostgreSQL DROP COLUMN: Remove One or More Columns of a Table
  • A Quick Guide To The PostgreSQL TIME Data Type
  • A Comprehensive Look at PostgreSQL Interval Data Type

More Tutorials

  • PostgreSQL Cheat Sheet
  • PostgreSQL Administration
  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2017 by PostgreSQL Tutorial Website. All Rights Reserved.