PostgreSQL – CURRENT_TIMESTAMP Function

In PostgreSQL, the CURRENT_TIMESTAMP function is used to retrieve the current date and time along with the time zone information. It returns the current timestamp in the timestamp with time zone data type, which includes the date, time, seconds, milliseconds, and the time zone offset.

The basic syntax of the CURRENT_TIMESTAMP function is as follows:

SELECT CURRENT_TIMESTAMP;

Here’s an example of using the CURRENT_TIMESTAMP function:

SELECT CURRENT_TIMESTAMP AS current_datetime;

This query will return the current date and time in the format YYYY-MM-DD HH:MI:SS.MS+TZ, such as 2023-08-06 15:30:45.123456+00:00.

If you want to display the current timestamp in a specific time zone, you can use the AT TIME ZONE clause:

SELECT CURRENT_TIMESTAMP AT TIME ZONE 'America/New_York' AS current_datetime_ny;

In this example, the query will return the current date and time in the America/New_York time zone.

The CURRENT_TIMESTAMP function is especially useful when you need to work with the current date and time, record timestamps in a table, or perform time-related operations in your PostgreSQL queries.

If you only need the current date or time separately, you can use the CURRENT_DATE and CURRENT_TIME functions respectively.

Similar Posts