How to Get the Current Time Without Time Zone in PostgreSQL

PostgreSQL provides several DateTime functions to get the current date or time. For instance, the NOW() function is used to get the current DateTime; the CURRENT_DATE function is used to get the current date only; the CURRENT_TIME function retrieves the current time with time zone offset, etc. Similarly, to get the current time without time zone offset, the LOCALTIME function is used in Postgres.

This blog will guide you on getting the current time in Postgres without the time zone.

Getting the Current Time Without Timezone in Postgres?

The LOCALTIME function may take an optional precision parameter known as the “precision”, as shown in the following syntax:

LOCALTIME(precision);

The “precision” parameter determines the fractional points for the seconds' field. Skipping the precision parameter will retrieve the time with full available precision. The return type of the stated function is “TIME(TIME WITHOUT TIME ZONE)”.

Example 1: How to Get Current Time in PostgreSQL Without Time Zone Via LOCALTIME?

Use the LOCALTIME function with the help of a SELECT statement to fetch the current time without the time zone:

SELECT LOCALTIME;
img

The output shows that the stated function retrieves the time(at which the transaction begins) without a timezone.

Example 2: How to Get Current Time With Specific Precision in PostgreSQL?

Pass an integer as an argument to the LOCATIME function to get the time(seconds) up to specified precision:

SELECT LOCALTIME, LOCALTIME(2);
img

The output shows the difference between the local time without precision parameter and with precision parameter.

Example 3: How to Set the Current Time Without Time Zone As the Column Default Value?

We have created a sample table named “flight_details” with three columns, as shown in the below snippet:

img

Let’s add the current time without the time zone as the default value of the “arrival_time” column:

ALTER TABLE flight_details
ALTER COLUMN arrival_time SET DEFAULT LOCALTIME;
img

The selected table has been altered successfully. Let’s add a new record to the “flight_details” table:

INSERT INTO flight_details(flight_num, departure_time)
VALUES (1001, '07:35:40');
img

A new record has been inserted into the flight_details table. We didn’t specify any value for the “arrival_time” column. Let’s execute the SELECT query to see what the output says:

SELECT * FROM flight_details;
img

The output demonstrates that postgres assigned the current time(without time zone) to the arrival_time column. This way, you can set the current time without the time zone as the default value of a specific column.

Conclusion

Postgres offers a DateTime function named LOCALTIME that retrieves the current time without a timezone. The LOCALTIME function may or may not accept an optional argument named “precision”, which determines the decimal places for the seconds' field. Skipping the precision parameter will retrieve the time with full available precision. The return type of the stated function is TIME(TIME WITHOUT TIME ZONE). This Postgres blog presented a comprehensive guide on getting the current time without time zone information in PostgreSQL using the LOCALTIME function.