Postgres current_timestamp() function
Get the current date and time
The Postgres current_timestamp()
function returns the current date and time with timezone. The now()
function is an alias.
This function is particularly useful for timestamping database entries, calculating time differences, or implementing time-based business logic. For example, you can use it to record the time a user logs in, or when the status of a purchase order changes. Fetching the current time information can also be used to calculate time-based metrics and schedule periodic tasks.
Function signature
The current_timestamp()
function has two forms:
This form returns the current timestamp with timezone at the start of the current transaction. Note that there are no parentheses in this form.
precision
(optional): An integer specifying the number of fractional digits in the seconds field. It can range from 0 to 6. If omitted, the result has the full available precision.
Example usage
Let's consider a table called user_logins
that tracks user login activity. We can use current_timestamp
to record the exact time a user logs in.
This INSERT
query adds a new login record with the current timestamp.
The SELECT
query retrieves the login record, showing the user ID and the timestamp of the login.
We can also specify current_timestamp
as the default value for a timestamp column when creating the table. For example, consider the query below, where we set up a table to track purchase orders and add some records:
This query creates a table to store purchase orders, with the order_date
column set to the current timestamp by default. When inserting new records, the order_date
column will automatically be populated with the current timestamp.
This query retrieves all purchase orders, showing the order ID and the timestamp when each order was created.
Advanced examples
current_timestamp
to query recent data
Use We can use current_timestamp
in a SELECT
statement to compare with stored timestamps and fetch recent records. For example, to retrieve all login records from the past 6 hours, you can use current_timestamp
in the WHERE
clause:
This query retrieves all logins from the past 6 hours and calculates how long ago each login occurred.
current_timestamp
Specify timestamp precision for You can specify the precision of the timestamp when needed:
This query computes the current timestamp value with different levels of precision: milliseconds, microseconds, and without fractional seconds.
current_timestamp
with triggers
Use You can use current_timestamp
in combination with a default value and an update trigger to automatically maintain creation and modification timestamps for records. For example, run the following query to create a table storing articles for a blog:
This query creates a table to store articles, with columns for the title, content, and creation and update timestamps. It also defines a trigger that updates the updated_at
column whenever an article is modified. To verify, run the following query that updates the content for the first article:
This query returns the following output, showing the updated content and the update timestamp for the first article:
Additional considerations
Timezone awareness
current_timestamp
returns a value in the timezone of the current session, which defaults to the server's timezone unless explicitly set in the session. This is important to note when working with timestamps across different timezones.
Alternative functions
now()
- An alias forcurrent_timestamp
.transaction_timestamp()
- Returns the current timestamp at the start of the current transaction. Equivalent tocurrent_timestamp
.statement_timestamp()
- Returns the current timestamp at the start of the current statement.clock_timestamp()
- Returns the current timestamp, changing even within a single SQL statement.