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.
Similar Reads
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
SQL Interview Questions
Are you preparing for a SQL interview? SQL is a standard database language used for accessing and manipulating data in databases. It stands for Structured Query Language and was developed by IBM in the 1970s, SQL allows us to create, read, update, and delete data with simple yet effective commands.
15+ min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands
SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
SQL Tutorial
SQL is a Structured query language used to access and manipulate data in databases. SQL stands for Structured Query Language. We can create, update, delete, and retrieve data in databases like MySQL, Oracle, PostgreSQL, etc. Overall, SQL is a query language that communicates with databases. In this
11 min read
SQL Joins (Inner, Left, Right and Full Join)
SQL joins are fundamental tools for combining data from multiple tables in relational databases. Joins allow efficient data retrieval, which is essential for generating meaningful observations and solving complex business queries. Understanding SQL join types, such as INNER JOIN, LEFT JOIN, RIGHT JO
6 min read
Normal Forms in DBMS
In the world of database management, Normal Forms are important for ensuring that data is structured logically, reducing redundancy, and maintaining data integrity. When working with databases, especially relational databases, it is critical to follow normalization techniques that help to eliminate
8 min read
Class Diagram | Unified Modeling Language (UML)
A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
ACID Properties in DBMS
In the world of Database Management Systems (DBMS), transactions are fundamental operations that allow us to modify and retrieve data. However, to ensure the integrity of a database, it is important that these transactions are executed in a way that maintains consistency, correctness, and reliabilit
8 min read
Spring Boot Tutorial
Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Introduction of DBMS (Database Management System)
A Database Management System (DBMS) is a software solution designed to efficiently manage, organize, and retrieve data in a structured manner. It serves as a critical component in modern computing, enabling organizations to store, manipulate, and secure their data effectively. From small application
8 min read