Last chance! 50% off DataCamp Premium
Sale ends in
PostgreSQL provides various date functions to handle and manipulate date and time values. Among these, `CURRENT_TIMESTAMP` is used to obtain the current date and time, with the time zone information, at the moment the function is executed.
The `CURRENT_TIMESTAMP` function is used when you need to capture the exact moment an operation is performed, such as logging events or setting default values for timestamp columns.
sql
SELECT CURRENT_TIMESTAMP;
This syntax retrieves the current date and time, including the time zone, at the point this query is executed.
sql
SELECT CURRENT_TIMESTAMP;
This command returns the current date and time, including time zone information, at the moment of execution.
sql
INSERT INTO logs (event_description, event_time)
VALUES ('User logged in', CURRENT_TIMESTAMP);
This example demonstrates inserting the current timestamp into the `logs` table, capturing the exact time an event occurs.
sql
CREATE TABLE orders (
order_id SERIAL PRIMARY KEY,
order_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Here, the `orders` table is created with a column `order_time` that automatically stores the current timestamp as its default value when a new record is inserted.
sql
SELECT CURRENT_TIMESTAMP(2);
This retrieves the current timestamp with precision up to two fractional seconds.