EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials PostgreSQL Tutorial PostgreSQL CURRENT_TIMESTAMP()

PostgreSQL CURRENT_TIMESTAMP()

Sohel Sayyad
Article bySohel Sayyad
Priya Pedamkar
Reviewed byPriya Pedamkar

Updated May 29, 2023

PostgreSQL CURRENT_TIMESTAMP()

Introduction to PostgreSQL CURRENT_TIMESTAMP()

PostgreSQL CURRENT_TIMESTAMP() is used to return the current date and time with time zone, it will display the time when our transaction starts. The PostgreSQL SQL standard function ‘current_timestamp’ returns values based on the start time of the current transaction. It retrieves the start time of the current transaction and provides the corresponding timestamp information. It retrieves the current transaction’s start time and provides the corresponding timestamp information. Current timestamp and transaction timestamp functions are equivalent, but the current timestamp will return the current date and time, and the transaction timestamp will return the transaction start time. Current timestamp, transaction timestamp, and now function in PostgreSQL are doing the same, but the current timestamp is a syntactical oddity function.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Syntax

Below is the syntax of the current_timestamp function in PostgreSQL.

Current_timestamp;

OR

Current_timestamp (<Precision>)

Parameter Description

Below is the parameter description syntax of the current timestamp function in PostgreSQL.

Current timestamp: Current timestamp in PostgreSQL will return the current date and time. The SQL-Standard function in PostgreSQL that returns values based on the start time of the current transaction is ‘current_timestamp’. It retrieves the current date and time, including the time zone, at the beginning of the ongoing transaction.

Precision: The current timestamp function in PostgreSQL allows an optional parameter to specify the precision of fractional seconds in the result. The precision determines the number of digits in the fractional seconds portion of the timestamp. If we do not use the precision argument in the current timestamp function, it will return a timestamp with time zone this includes include the full fractional second’s precision in PostgreSQL.

How does PostgreSQL CURRENT_TIMESTAMP() function work?

Below is the working of the current timestamp in PostgreSQL:

  • PostgreSQL utilizes the current timestamp to retrieve the current date and time, including the time zone. It provides the current timestamp information in PostgreSQL.
  • We can use the precision parameter with the current timestamp function in PostgreSQL.
  • In PostgreSQL, the internally current timestamp will work as now function in PostgreSQL. The current timestamp and now function is similar to work in PostgreSQL.
  • Transaction timestamp and the current timestamp is equivalent to each other in PostgreSQL. But the transaction timestamp will reflect the same, which the function returned.

Examples to Implement PostgreSQL CURRENT_TIMESTAMP()

The below example shows that now, the current timestamp and transaction timestamp function work similarly to each other:

Example #1

Code:

SELECT CURRENT_TIMESTAMP;
SELECT now();
SELECT transaction_timestamp();

Output:

PostgreSQL CURRENT_TIMESTAMP()1

Explanation: The above example shows the time and timestamp of all three functions working is the same. The current timestamp is used as the default timestamp value of a column in PostgreSQL. The current timestamp and the current time are to deliver the values with the time zone in PostgreSQL. The start time of the current statement is the time of the latest command received from the client. The current timestamp is useful and important in PostgreSQL to return the date and timestamp with the time zone. In PostgreSQL, we can assign the default value of the current timestamp to a column using the “default” keyword during table creation. This eliminates the need to manually insert the current timestamp value in every insert statement, as the current timestamp function will automatically populate the column with the current date and time in a specified format.

‘YYYY-MM-DD HH:MM: SS.US+TZ’

Example #2

The below example shows the format of the current timestamp function in PostgreSQL:

Code:

SELECT CURRENT_TIMESTAMP;

Output:

PostgreSQL CURRENT_TIMESTAMP()2

Explanation: In the above example, the current timestamp format is below.

  • YYYY – It is defined as the year in four-digit.
  • MM – It defines the month in two digits. The range is 01 to 12.
  • DD – It is defined as today’s date in two digits. The range is 01 to 31.
  • HH – It is defined as the current hour in two digits. The range is 00 to 23.
  • MM – It is defined as the current minute in two digits. The range is 00 to 59.
  • SS – It is defined as the current second in two digits. The range is 00 to 59.
  • TZ – It is defined as the time zone in the current timestamp function.
  • We have not need to use () in the current timestamp function when we have not used the precision parameter.

Example #3

If we have used () without a precision parameter, it will show a syntax error. The below example shows that we do not need to use () when we have not used the precision parameter:

Code:

SELECT CURRENT_TIMESTAMP;

Output:

PostgreSQL CURRENT_TIMESTAMP()3

Example #4

Current timestamp with precision value:

 In the below example, we have used a precision value of 5 with the current timestamp. We have used five fractional seconds to define the current timestamp.

Code:

SELECT CURRENT_TIMESTAMP (5);

Output:

PostgreSQL CURRENT_TIMESTAMP()4

Example #5

Current timestamp without using precision value:

In the below example, we have not used precision value with the current timestamp. We did not use () because we have not defined the precision value in the example below.

Code:

SELECT CURRENT_TIMESTAMP;

Output:

precision value

Example #6

Use the current timestamp in the column as the default value:

In the below example, we have used the current timestamp as the default value in the student table. In the below example, we have created the default current timestamp on the admission_date column.

Code:

CREATE TABLE student (stud_id serial PRIMARY KEY, stud_address varchar(255) NOT NULL, stud_name varchar(255) NOT NULL, admission_date TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP);

Output:

default current timestamp

Example #7

After creating the student table, we do not need to insert a value in the column name as admission_date. It will take the automatic value as the current timestamp at the time of insertion as follows.

Code:

INSERT INTO STUDENT (stud_id, stud_address, stud_name) VALUES (1, 'Pune', 'ABC');
INSERT INTO STUDENT (stud_id, stud_address, stud_name) VALUES (2, 'Mumbai', 'PQR');
select * from STUDENT;

Output:

automatic value

Recommended Articles

We hope that this EDUCBA information on “PostgreSQL CURRENT_TIMESTAMP()” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. PostgreSQL GRANT
  2. Trunc() PostgreSQl
  3. PostgreSQL FULL OUTER JOIN
  4. PostgreSQL replace
All in One Excel VBA Bundle
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
Financial Analyst Masters Training Program
2000+ Hours of HD Videos
43 Learning Paths
550+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Data Science Bundle
2000+ Hour of HD Videos
80 Learning Paths
400+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Software Development Bundle
5000+ Hours of HD Videos
149 Learning Paths
1050+ Courses
Verifiable Certificate of Completion
Lifetime Access
Primary Sidebar
Popular Course in this category
POSTGRESQL Course Bundle - 5 Courses in 1 | 1 Mock Test
 15+ Hour of HD Videos
5 Courses
1 Mock Tests & Quizzes
  Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more