Open In App

PostgreSQL – CURRENT_TIME Function

Last Updated : 17 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The PostgreSQL CURRENT_TIME function returns the current time and the current time zone. This function is handy when you need to work with time-sensitive data in your database applications.

Let us better understand the CURRENT_TIME Function in PostgreSQL from this article.

Syntax

CURRENT_TIME(precision)

Parameter:

  • Precision Argument: The precision argument sets the precision of the returned ‘TIME' type value in fractional seconds. If no precision is specified, the function returns the full available precision by default.

Return Value:

  • The ‘CURRENT_TIME' function returns a ‘TIME WITH TIME ZONE' value, representing the current time along with the current time zone.

PostgreSQL CURRENT_TIME Function Examples

Let us take a look at some of the examples of CURRENT_TIME Function in PostgreSQL to better understand the concept.

Example 1: Getting the Current Time

The following statement can be used to get the current time.

Query:

SELECT CURRENT_TIME;

Output:

PostgreSQL CURRENT_TIME Function Example

Example 2: Using CURRENT_TIME with Precision

The following statement shows the process of using the CURRENT_TIME function with the precision of 2.

Query:

SELECT CURRENT_TIME(2);

Output:

PostgreSQL CURRENT_TIME Function Example

Example 3: Using CURRENT_TIME as a Default Value

The CURRENT_TIME function can also be used as the default value of the TIME columns. To demonstrate this, create a table named ‘log’:

CREATE TABLE log (
log_id SERIAL PRIMARY KEY,
message VARCHAR(255) NOT NULL,
created_at TIME DEFAULT CURRENT_TIME,
created_on DATE DEFAULT CURRENT_DATE
);

The ‘log’ table has the ‘created_at’ column whose default value is the value returned by the ‘CURRENT_TIME’ function. Now insert some data to the ‘demo’ table:

INSERT INTO log( message )
VALUES('Testing the CURRENT_TIME function');

Now verify if the row was inserted into the log table with the created_at column added correctly by using the following query:

SELECT * FROM log;

Output:

PostgreSQL CURRENT_TIME Function Example

Alternative: Using the PostgreSQL NOW Function

We can get similar output by using by Postgresql NOW function.

Syntax: NOW();

This will return the current date and time in the following format: ‘YYYY-MM-DD HH:MI: SS.MS+TZ’

Example 1: Getting the Current Date and Time

The following statement can be used to get the current time:

SELECT NOW();

Output:

PostgreSQL NOW Function

Output

Important Points About PostgreSQL CURRENT_TIME Function

  • If no precision is specified, CURRENT_TIME returns the time with the full available precision of the underlying data type, which can include microseconds.
  • In the context of functions and triggers, CURRENT_TIME returns the time at the start of the transaction, not the actual current time at the moment of the call.
  • The CURRENT_TIME function returns the current time along with the current time zone. This is different from the CURRENT_TIME without time zone.
  • While CURRENT_TIME returns only the time part with time zone, CURRENT_TIMESTAMP and NOW() return the full date and time.


Next Article

Similar Reads

three90RightbarBannerImg