Updated May 12, 2023
Introduction to PostgreSQL CURRENT_TIME
PostgreSQL provides various functions for manipulating time values. It is one of them used to get the current time of day. It returns the current time value of the day with the time zone in which it is called. Even if the CURRENT_TIME() is a function but while using the CURRENT_TIME() function, it is not necessary to define the parentheses () if we are not specifying the precision parameter. The returned time format of the PostgreSQL CURRENT_TIME() function is a ‘HH:MM: SS.US+TZ.’ We can use this function with PostgreSQL 8.4, PostgreSQL 9.0, PostgreSQL 9.1, PostgreSQL 9.2, PostgreSQL 9.3, and PostgreSQL 9.4, etc. versions.
Syntax:
Given below is the syntax:
current_time( [ precision ] )
Explanation:
- precision: It is an optional parameter. The value defined is used as fractional seconds precision. If the precision is not specified, then the PostgreSQL CURRENT_TIME() function returns the day’s current time with the full available precision.
How PostgreSQL CURRENT_TIME() Function work in PostgreSQL?
- It is used to get the current time of day with the time zone.
- The returned time format of the PostgreSQL CURRENT_TIME() function is a ‘HH:MM:SS.US+TZ.’
- It takes precision as an optional parameter used as fractional seconds precision. If the precision is not defined, then the PostgreSQL CURRENT_TIME() function returns the day’s current time with the full available precision.
Code:
SELECT current_time AS current_day_time;
Output:
Examples of PostgreSQL CURRENT_TIME
Given below are the examples with and without precision defined:
Example #1
Code:
SELECT current_time(1) AS current_day_time_prec_1;
Output:
Example #2
Code:
SELECT current_time(2) AS current_day_time_prec_2;
Output:
Example #3
Code:
SELECT current_time(3) AS current_day_time_prec_3;
Output:
Example #4
We can also use the PostgreSQL CURRENT_TIME() function in table creation. We can give a default value to the column. It will have different time values as per the execution time of this function.
Consider the following example where we will create a table named ‘Employee,’ and this table will have a column ‘creation_time’ with data type as TIME. The column ‘creation_time’ will have a default value set to the current time as we use the PostgreSQL CURRENT_TIME() function to set a default value for the PostgreSQL CURRENT_TIME() part.
Consider the following CREATE TABLE statement, which will create a table named ‘Employee.’
Code:
CREATE TABLE Employee(
Employee_id serial PRIMARY KEY,
Employee_name varchar(255) NOT NULL,
creation_time TIME DEFAULT CURRENT_TIME
);
Now, we will insert a row into the Employee table by using the INSERT INTO statement as follows:
Code:
INSERT INTO Employee(Employee_name)
VALUES('Jacob Petter');
In the above INSERT INTO statement, we have not specified the creation time; hence the, PostgreSQL sets the default value as the current time.
Illustrate the result of the Employee table by using the following SQL statement and a snapshot.
Code:
SELECT * FROM Employee;
Output:
Now, we will again insert a row into the Employee table by using the INSERT INTO statement as follows:
Code:
INSERT INTO Employee(Employee_name)
VALUES('David Bravo');
Illustrate the result of the Employee table by using the following SQL statement and a snapshot.
Code:
SELECT * FROM Employee;
Output:
As per the SQL query execution time, the Employee table will have different time values inserted in the ‘creation_time’ column. If someone is joining a company in the morning and someone is joining in the afternoon, and we execute the insert into the SQL statement as given above, joining will be as per the SQL query execution time.
Recommended Articles
We hope that this EDUCBA information on “PostgreSQL CURRENT_TIME” was beneficial to you. You can view EDUCBA’s recommended articles for more information.