PostgreSQL localtimestamp() Function
The PostgreSQL localimestamp()
function returns the system date and time when the transaction started.
localtimestamp()
Syntax
Here is the syntax of the PostgreSQL localtimestamp()
function:
localtimestamp -> TIMESTAMP
or
localtimestamp(precision) -> TIMESTAMP
Parameters
precision
-
Required. It is an integer indicating the precision of fractional seconds.
Return value
The PostgreSQL localtimestamp()
function returns the system date and time when the transaction started.
That is, all localtimestamp()
functions in a transaction return the same value, that is different from clock_timestamp()
.
Unlike current_timestamp()
, the localtimestamp()
function returns the system date and time without time zone information.
localtimestamp()
Examples
This example shows how to use the PostgreSQL localtimestamp()
function to get the current date and time.
SELECT localtimestamp;
localtimestamp
----------------------------
2022-05-16 20:56:24.432821
Or use the following statement to limit the precision of fractional seconds:
SELECT
localtimestamp(0),
localtimestamp(1),
localtimestamp(2),
localtimestamp(3),
localtimestamp(4),
localtimestamp(5),
localtimestamp(6);
localtimestamp | 2022-05-16 20:56:37
localtimestamp | 2022-05-16 20:56:36.7
localtimestamp | 2022-05-16 20:56:36.71
localtimestamp | 2022-05-16 20:56:36.707
localtimestamp | 2022-05-16 20:56:36.7073
localtimestamp | 2022-05-16 20:56:36.70727
localtimestamp | 2022-05-16 20:56:36.707265
The localtimestamp()
function returns the time when the transaction started, not the time when the function was executed. See the example below:
SELECT
localtimestamp,
pg_sleep(1),
localtimestamp;
-[ RECORD 1 ]--+---------------------------
localtimestamp | 2022-05-16 20:56:52.244352
pg_sleep |
localtimestamp | 2022-05-16 20:56:52.244352
Here, even though we use pg_sleep(1)
to pause execution for 1 second between the two localtimestamp
functions, they still return the same value.