PostgreSQL CURRENT_DATE Function
Summary: in this tutorial, you will learn how to use the PostgreSQL CURRENT_DATE
function to get the current date.
Introduction to the PostgreSQL CURRENT_DATE function
The CURRENT_DATE
function returns the current date in the default time zone of the database session.
Here is the basic syntax of the CURRENT_DATE
function:
The CURRENT_DATE
function returns a DATE
value that represents the current date.
Note that the CURRENT_DATE
function returns the current date without any time information.
PostgreSQL CURRENT_DATE function examples
Let’s explore some examples of using the CURRENT_DATE
function.
1) Basic PostgreSQL CURRENT_DATE function example
The following example shows how to use the CURRENT_DATE
function to get the current date:
Output:
2) Using the PostgreSQL CURRENT_DATE for date-based filtering
You can use the CURRENT_DATE
in the WHERE
clause to filter data based on the current date.
For example, you can retrieve the rentals placed today by comparing the rental date in the rental
table with the result of the CURRENT_DATE
function:
3) Calculating ages
First, create a new table called employees
with the date_of_birth
column and insert some data into the table:
Output:
Second, calculate the age of employees using the CURRENT_DATE
function:
Output:
4) Using the PostgreSQL CURRENT_DATE function as the default value of a column
In practice, you often use the CURRENT_DATE
function as a default value of a column. For example:
First, create a table called delivery
:
In the delivery
table, the delivery_date
is set with the default value generated by the CURRENT_DATE
function.
Second, insert a new row into the delivery
table:
In this INSERT
statement, we do not specify the delivery date. Therefore, PostgreSQL uses the current date as the default value.
Third, verify the insert:
Output:
The output indicates that the statement inserted the current date into the delivery_date
column.
Note that you will see a different value in the delivery_date
column, depending on when you execute the query.
Summary
- Use the PostgreSQL
CURRENT_DATE
function to retrieve the current date.