PostgreSQL NOW() Function With Practical Examples

PostgreSQL provides a variety of built-in date and time functions that finds the date and time with or without timezone. For instance, an inbuilt function named NOW() is used to get today’s date and time with the time zone.

The purpose of this blog post is to demonstrate how to get the current/present date and time in Postgres using the NOW() function. So, let’s start!

How to Use the NOW() Function in PostgreSQL?

In Postgres, the NOW() retrieves the current time and date along with the time zone(based on the database server’s setting). It doesn’t require any argument, as shown in the following syntax:

NOW();

For a deeper understanding of the NOW() function, let's implement it practically.

Example 1: How Does the NOW() Function Work in Postgres?

This particular example will show you the basic usage of the NOW() function in Postgres:

SELECT NOW();
img

The output proves that the stated function retrieves the current date, time, and timezone.

Example 2: How to Use the NOW() Function to Get Today’s Date and Time Without Timezone?

Use the typecast operator “::” to get the current/present date and time using the NOW() function but without a timezone:

SELECT NOW() :: TIMESTAMP;
img

The output proves that this time, the stated function retrieves the current date, and time, without a timezone.

Example 3: How Do I Set Today’s Date as the Default Value of a Column?

Firstly create a new sample table, let’s say “submit_articles”. We will create three columns within that table: article_id, article_title, and submission_date. Suppose we want to set today’s date as the default submission date. To do so, we will set the NOW() function as a default value of the submission_date column:

CREATE TABLE submit_articles(
article_id INTEGER PRIMARY KEY, 
article_title TEXT, 
submission_date DATE DEFAULT NOW());
img

The submit_articles table has been created. Let’s validate the table’s creation via the below command:

SELECT * FROM submit_articles;
img

Let’s insert some records into the submit_articles table via the INSERT INTO statement:

INSERT INTO submit_articles(article_id, article_title)
VALUES(1, 'Postgres DROP CASCADE'),
(2, 'Postgres DROP TABLE'),
(3, 'Postgres DROP DATABASE');
img

The specified records have been inserted into the submit_articles table. In the insert query, you can see that no record was inserted in the submission_date column. Executing the SELECT command will fetch the newly inserted records:

SELECT * FROM submit_articles;
img

The output depicts that Postgres specifies the current date as a default value in the submission_date column. This way, you can specify the current/today’s date as a default value of any table’s column using the NOW() function.

Conclusion

PostgreSQL provides an inbuilt function named NOW() that is used to get today’s date and time along with the time zone. In Postgres, the NOW() function retrieves the current time and date along with the time zone(based on the database server’s setting). This function doesn’t require any argument/parameter. This blog post demonstrated the working of the NOW() function via practical examples.