Movatterモバイル変換


[0]ホーム

URL:


Add Neon Auth to your app without leaving Cursor or Claude
/Date Functions/CURRENT_TIME

PostgreSQL CURRENT_TIME Function

Summary: in this tutorial, you will learn how to use the PostgreSQLCURRENT_TIME function to get the current time with the timezone.

Introduction to the PostgreSQL CURRENT_TIME function

The following illustrates the syntax of theCURRENT_TIME function:

CURRENT_TIME(precision)

TheCURRENT_TIME function accepts one optional argumentprecision.

Theprecision specifies the returned fractional seconds precision. If you omit theprecision argument, the result will include the full available precision.

TheCURRENT_TIME function returns aTIME WITH TIME ZONE value that represents the current time with the timezone.

PostgreSQL CURRENT_TIME function examples

Let’s explore some examples of using theCURRENT_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:

SELECT CURRENT_TIME;

The output is aTIME WITH TIME ZONE value as follows:

current_time-------------------- 14:42:10.884946-07(1 row)

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 theCURRENT_TIME function with the precision set to 2:

SELECT CURRENT_TIME(2);

Output:

current_time---------------- 14:44:35.03-07(1 row)

3) Using the CURRENT_TIME function as the default value of a column

TheCURRENT_TIME function can be used as the default value ofTIME columns. For example:

First,create a table calledlog:

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

Thelog table has thecreated_at column with the default value is the result of theCURRENT_TIME function.

Second,insert a row into thelog table:

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

In the statement, we only specify a value for themessage column. Therefore, other columns will take the default values.

Third, check whether the row was inserted into thelog table with thecreated_at column populated correctly by using the followingquery:

SELECT * FROM log;

The following picture shows the result:

id |              message              |   created_at    | created_on----+-----------------------------------+-----------------+------------  1 | Testing the CURRENT_TIME function | 14:46:28.188809 | 2024-01-26(1 row)

The output indicates that thecreated_at column is populated with the time at which theINSERT statement executed.

Summary

  • Use the PostgreSQLCURRENT_TIME function to get the current time with the default timezone.

Last updated on

Was this page helpful?
Thank you for your feedback!

[8]ページ先頭

©2009-2025 Movatter.jp