PostgreSQL - CURRENT_TIMESTAMP Function
Last Updated :
20 Aug, 2024
PostgreSQL provides the 'CURRENT_TIMESTAMP() function, which returns the current date and time, along with the time zone, at the precise moment the transaction begins. This function is often used for tracking the creation or modification of records in a database.
Here, we’ll look into the syntax, usage, and examples of the CURRENT_TIMESTAMP() function to help you better understand how to leverage it in your PostgreSQL database.
CURRENT_TIMESTAMP() Function in PostgreSQL
The CURRENT_TIMESTAMP() function returns the current date and time with time zone information from when the transaction started. It’s important to note that the timestamp reflects the time at the beginning of the transaction, not when the function is called. This feature is particularly useful when you need consistent timestamps for all operations within a single transaction.
Syntax:
CURRENT_TIMESTAMP(precision)
Parameters:
- The precision is used to set the number of digits in the fractional seconds precision in the second field of the returned query results. If passes without the precision, it returns the current time (TIMESTAMP type value) that includes the full fractional seconds precision available.
Return Value:
- The CURRENT_TIMESTAMP() function returns a TIMESTAMP WITH TIME ZONE that represents the date and time at which the transaction started.
PostgreSQL CURRENT_TIMESTAMP Function Examples
Let us take a look at some of the examples of CURRENT_TIMESTAMP Function in PostgreSQL to better understand the concept.
Example 1: Basic Usage of CURRENT_TIMESTAMP()
The following statement depicts the use of the CURRENT_TIMESTAMP() function to query for the current date and time:
SELECT CURRENT_TIMESTAMP;
Output:

Explanation: In this example, the timestamp includes the full precision of fractional seconds, along with the time zone.
Example 2: Creating a Table with CURRENT_TIMESTAMP as the Default Value
First create a table named note that has the 'created_at' column is a TIMESTAMP WITH TIME ZONE column.
CREATE TABLE note(
note_id serial PRIMARY KEY,
message varchar(255) NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
The 'created_at' column gets its default value from the result of the CURRENT_TIMESTAMP() function. Now, insert some data in the table:
INSERT INTO note(message)
VALUES('Testing current_timestamp function');
Third, verify whether the insert has been taken place correctly using the following query:
SELECT * FROM note;
Output:

Explanation: In this output, you can see that the 'created_at' column correctly reflects the timestamp when the row was added.
Similar Reads
PostgreSQL - CURRENT_TIME Function The PostgreSQL CURRENT_TIME function returns the current time and the current time zone. This function is handy when you need to work with time-sensitive data in your database applications.Let us better understand the CURRENT_TIME Function in PostgreSQL from this article.SyntaxCURRENT_TIME(precision
2 min read
PostgreSQL TO_TIMESTAMP() Function In PostgreSQL, managing and manipulating date and time values is important, especially when they are stored as strings. The to_timestamp function allows us to convert textual representations of dates and times into a valid timestamp format and making it easier to work with them in queries, calculati
5 min read
PostgreSQL - CURRENT_DATE Function The PostgreSQL CURRENT_DATE function is used to retrieve the current date. Itâs a simple and effective way to ensure your database operations are using the correct date, particularly for applications that need timestamp records.Let us better understand the CURRENT_DATE Function in PostgreSQL from th
2 min read
PostgreSQL - AGE Function In PostgreSQL, the AGE() function is a powerful tool for calculating the difference between two TIMESTAMP values. This function is especially useful for determining the age of individuals or the duration between events. Let us better understand the AGE() Function in PostgreSQL from this article.Synt
2 min read
PostgreSQL - DATE_TRUNC Function The PostgreSQL DATE_TRUNC() function is a powerful tool that helps us truncate a timestamp or interval to a specified level of precision. It is particularly useful when dealing with time-based data for effective data grouping and manipulation, making it ideal for reporting, analytics, and queries wh
4 min read
PostgreSQL Date Functions PostgreSQL is widely recognized for its comprehensive support for date and time manipulations, making it an excellent choice for applications requiring precise time management and complex calculations. This article explores the core PostgreSQL date functions, covering how to retrieve the current dat
4 min read