PostgreSQL – NOW Function

In PostgreSQL, the NOW() function is used to fetch the current date and time according to the server’s clock. It returns the current timestamp in the timestamp with time zone format. The NOW() function is commonly used when you want to record the current date and time in a database table or perform date and time-related operations.

Below is the basic syntax of the NOW() function:

SELECT NOW();

The above query will return the current timestamp in the following format: YYYY-MM-DD HH:MI:SS.MS+TZ.

For example, the result might look like this: 2023-08-06 15:30:45.123456+00:00

If you want to store the current timestamp in a table, you can use the NOW() function as part of an INSERT statement:

INSERT INTO your_table (column_name, timestamp_column)
VALUES ('some_value', NOW());

In this example, your_table is the name of your table, column_name is the name of the column where you want to insert some value, and timestamp_column is the column where you want to store the current timestamp.

You can also use the NOW() function in combination with other date and time functions to perform various operations, like calculating the difference between two timestamps or extracting specific parts of the timestamp (e.g., year, month, day, etc.).

Here’s an example of calculating the difference between two timestamps:

SELECT NOW() - '2022-12-25 10:30:00' AS time_difference;

This will return the time difference between the current timestamp and the specified timestamp.

Similar Posts