PostgreSQL CURRENT_TIME Function
Summary: in this tutorial, you will learn how to use the PostgreSQL CURRENT_TIME
function to get the current time with the timezone.
Introduction to the PostgreSQL CURRENT_TIME function
The following illustrates the syntax of the CURRENT_TIME
function:
The CURRENT_TIME
function accepts one optional argument precision
.
The precision
specifies the returned fractional seconds precision. If you omit the precision
argument, the result will include the full available precision.
The CURRENT_TIME
function returns a TIME WITH TIME ZONE
value that represents the current time with the timezone.
PostgreSQL CURRENT_TIME function examples
Let’s explore some examples of using the CURRENT_TIME
function.
1) Basic PostgreSQL CURRENT_TIME function example
The following example uses the CURRENT_TIME function to get the current time with the timezone:
The output is a TIME WITH TIME ZONE
value as follows:
In this example, we don’t specify the precision argument. Therefore, the result includes the full precision available.
2) Using the PostgreSQL CURRENT_TIME function with a precision example
The following example shows how to use the CURRENT_TIME
function with the precision set to 2:
Output:
3) Using the CURRENT_TIME function as the default value of a column
The CURRENT_TIME
function can be used as the default value of TIME
columns. For example:
First, create a table called log
:
The log
table has the created_at
column with the default value is the result of the CURRENT_TIME
function.
Second, insert a row into the log
table:
In the statement, we only specify a value for the message
column. Therefore, other columns will take the default values.
Third, check whether the row was inserted into the log
table with the created_at
column populated correctly by using the following query:
The following picture shows the result:
The output indicates that the created_at
column is populated with the time at which the INSERT
statement executed.
Summary
- Use the PostgreSQL
CURRENT_TIME
function to get the current time with the default timezone.