Postgres now() function
Get the current date and time
The Postgres now()
function returns the current date and time with timezone. It's an alias for the current_timestamp()
function.
This function is commonly used for timestamping database entries, calculating time differences, or implementing time-based logic in applications. For instance, you might use it to record when a user creates an account, when an order is placed, or to calculate intervals - like how long ago an event occurred.
Function signature
The now()
function has a single form:
This form returns the current timestamp with the timezone at the start of the current transaction.
Example usage
Let's consider a user_accounts
table that tracks user registration information. We can use now()
to record the exact time a user creates their account.
This query creates a table to store user account information, with the created_at
column automatically set to the current timestamp when a new record is inserted.
Let's insert another record and retrieve all user accounts:
This query returns the following output:
Advanced examples
now()
to calculate time differences
Use We can use now()
in combination with stored timestamps to calculate time differences. For example, let's create a table to track project deadlines and calculate how much time is left:
This query calculates and displays the remaining time for each project, ordered from the most to the least urgent.
now()
with triggers
Use We can use now()
in combination with an update trigger to automatically maintain modification timestamps for records.
Here's an example using a table for tracking customer orders. It has columns for both the creation and last update timestamps, with a trigger that updates the last_updated
column whenever an order is modified:
Now, let's update an order and observe the changes:
This query returns the following output, showing the updated status and the new last_updated
timestamp, for the modified order.
now()
in a function for date/time calculations
Use We can wrap now()
in a user-defined function to perform more complex date/time calculations. For example, here's a function that calculates the current age of a user.
This query calculates the age of three users based on their date of birth:
Additional considerations
Time zone awareness
Like current_timestamp
, now()
returns a value in the timezone of the current session. This defaults to the server's timezone unless explicitly set in the session. It's important to keep this in mind when working with timestamps across different timezones.
now()
and the keyword now
Difference between The now()
function is a built-in function that returns the current timestamp with the timezone. In contrast, the keyword now
(without parentheses) is a reserved word that is converted to the current timestamp value when first parsed.
It is recommended to use now()
for clarity and consistency. For example, if the default value for a column is set to now
, it will be evaluated once when the table is created and reused for all successive records. Whereas, now()
will be evaluated each time a new row is inserted, which is the typically desired behavior.
Alternative functions
current_timestamp()
- Functionally identical tonow()
.transaction_timestamp()
- Returns the current timestamp at the start of the current transaction, also equivalent tonow()
.statement_timestamp()
- Returns the current timestamp at the start of the current statement.clock_timestamp()
- Returns the actual current timestamp with timezone, which can change even during a single SQL statement.
Resources
Last updated on