PostgreSQL - NOW() Function
Last Updated :
10 Oct, 2024
The NOW
()
function in PostgreSQL is a powerful and essential tool for retrieving the current date and time. This is particularly useful when recording actions like adding or updating records in our database. Using PostgreSQL NOW
() function
, we can efficiently track when events occur, making the data more accurate and organized.
In this article, we will explain the NOW
()
function in PostgreSQL, with clear examples, usage scenarios, and explanations. We will also discuss how it can be used in real-world scenarios to enhance data management.
PostgreSQL NOW() Function
The NOW
()
function is commonly used to fetch the current timestamp (date and time). It is a timestamp with a time zone, which includes information about the current time zone of the PostgreSQL server. This feature is particularly important for applications that operate in multiple regions or need to store date and time information
Syntax:
NOW();
Key term:
- No parameters: The
NOW
()
function does not require any input parameters. It simply returns the current date and time.
Example of PostgreSQL NOW() Function
To better illustrate the use of the NOW
()
function, let’s create a simple table called orders
. This table will store information about customer orders, including the timestamp when each order was placed.
Query:
CREATE TABLE orders (
id SERIAL PRIMARY KEY,
customer_name VARCHAR(50),
order_time TIMESTAMP WITH TIME ZONE
);
INSERT INTO orders (customer_name, order_time) VALUES ('Alice', NOW());
INSERT INTO orders (customer_name, order_time) VALUES ('Bob', NOW());
INSERT INTO orders (customer_name, order_time) VALUES ('Charlie', NOW());
Output:
id | customer_name | order_time |
---|
1 | Alice | 2024-10-08 15:15:00+00 |
2 | Bob | 2024-10-08 15:15:00+00 |
3 | Charlie | 2024-10-08 15:15:00+00 |
Example 1: Basic Usage of NOW()
We can use the NOW
()
function to retrieve the current date and time with the given below Query. This is helpful when we want to log when a query or transaction occurs.
Query:
SELECT NOW() AS current_time;
Output:
OutputExplanation:
This command shows the current date and time in a format like YYYY-MM-DD HH:MM:SS+TZ, where TZ indicates the time zone. This gives a precise snapshot of the exact moment the command was executed.
Example 2: Using NOW() in INSERT Statements
One of the most common uses of the NOW
()
function is to record the exact time an event occurs. For example, when adding a new order to the orders
table, we can capture the current timestamp with the NOW
()
function.
Query:
INSERT INTO orders (customer_name, order_time)
VALUES ('Alice', NOW());
Output:
OutputExplanation:
This command adds a new order for the customer "Alice" and records the current date and time in the order_time field. By using NOW(), we ensure that the exact moment of the order is captured. This helps us to keep accurate records of customer activity.
Example 3: Using NOW() in SELECT with a Condition
You can also use the NOW() function to filter records based on time. For example, if we want to see orders placed in the last 30 minutes, we can run the below query
Query:
SELECT *
FROM orders
WHERE order_time > NOW() - INTERVAL '30 minutes';
Output:
OutputExplanation:
Assuming the current time is 2024-10-08 14:55:00, this command will show any orders made after 14:25:00. This command filters the orders to show only those placed within the last 30 minutes. By subtracting 30 minutes from NOW(), we set a time limit for the records we want to see, making it easy to analyze recent activity.
Conclusion
The NOW() function is an essential tool in PostgreSQL for anyone working with databases. It helps to quickly access the current date and time, allowing us to timestamp records effectively.
This is crucial for applications that need to keep track of when data is added or changed. By understanding how to use NOW(), we can manage data better and ensure it is up to date.
Ready to go from coding beginner to development pro? Our DSA to Development Coding Guide has everything you need to crush coding interviews and ace real-world projects! Limited spots available!