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 this article.
Syntax
CURRENT_DATE
Return value:
The ‘CURRENT_DATE’ function returns a ‘DATE‘ value that represents the current date.
PostgreSQL CURRENT_DATE Function Examples
Let us take a look at some of the examples of the CURRENT_DATE Function in PostgreSQL to better understand the concept.
Example 1: Retrieve the Current Date
The following statement shows how to use the ‘CURRENT_DATE’ function to get the current date:
SELECT CURRENT_DATE;
Output:
Example 2: Using CURRENT_DATE as a Default Value
The CURRENT_DATE function can be used as a default value of a column. So create a table named ‘delivery’ for demonstration:
CREATE TABLE delivery(
delivery_id serial PRIMARY KEY,
product varchar(255) NOT NULL,
delivery_date DATE DEFAULT CURRENT_DATE
);
INSERT INTO delivery(product)
VALUES('Data Structure And Algorithm Edition 1');
SELECT * FROM delivery;
Output:
Important Points About PostgreSQL CURRENT_DATE Function
- The
CURRENT_DATE
function returns the date according to the server’s current time zone setting but does not include the time part.CURRENT_DATE
is evaluated only once per transaction. This means that if you useCURRENT_DATE
multiple times in a single transaction, it will return the same value every time, which can be beneficial for consistency.- Unlike
NOW()
orCURRENT_TIMESTAMP
, which return the exact moment including time,CURRENT_DATE
is immutable within the context of a single query,CURRENT_DATE
can be used as a default value in table definitions, ensuring new rows have the correct date without needing to be explicitly set during insertion.