EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

PostgreSQL Timestamp

Home » Data Science » Data Science Tutorials » PostgreSQL Tutorial » PostgreSQL Timestamp

PostgreSQL Timestamp

Introduction to PostgreSQL Timestamp

PostgreSQL timestamp is used to store date and time format data into the database; timestamp automatically updates the timestamp each time when the row was modified or inserted into the table. If, in some case, if time zone of the server changes, it will not affect on actual data that we have stored in the database. Timestamp data type storage size is 8 bytes to storing timestamp into the database; the timestamp data type is beneficial and important in PostgreSQL to store the date and time data into the database.

Syntax:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

select now () :: timestamp;

select ‘date’ :: timestamp;

select ‘date: time’ :: timestamp;

select CURRENT_TIMESTAMP :: timestamp;

Below is the parameter description of the above syntax are as follows:

  • Select: Select is used to select timestamp value in timestamp syntax. We can select any date or time value to display the timestamp format in PostgreSQL. We can select the current date as now and the current timestamp function in PostgreSQL.
  • Timestamp: It is used to store date and time format data into the database; it is used to handle or store the date and time of data stored in the database. We can select date and time by using this data type.
  • Now (): This function is used to select the current date and time in PostgreSQL. If we want to use the current date and time, then we have used a now function in PostgreSQL. Now function is more important to select the current date and time in PostgreSQL.
  • Date and time: We can use any date and time to display the format of timestamp. If we have used the only date, then it will display a date and time.
  • Current timestamp: This function is used to select the current date and time in PostgreSQL. If we want to use the current date and time, then we have to use a current timestamp function in PostgreSQL. The current timestamp function is more important to select the current date and time in PostgreSQL.

How does Timestamp Data Type work in PostgreSQL?

  • We can use a timestamp data type with and without a time zone. If we want to use a without a time zone, we need to use a timestamp data type; if we want to use a time zone, we need to use a timestamp data type.
  • Basically, timestamp data type divided into types with and without a time zone.
  • Below is the function of the timestamp data type is as follows.
  1. Now ()
  2. Current_timestamp
  3. Timeofday ()
  • Now function is used to select the current date and time in PostgreSQL. If we want to use the current date and time, then we need to use a now function in PostgreSQL. Now function is more important to select the current date and time in PostgreSQL.

Below figure shows the example of now function in PostgreSQL:

Code:

select now();

Output:

postgreSQL Timestamp 1

  • The time of day function is used to select todays day, date and time format with timezone.

The below figure shows the example of a time of day function in PostgreSQL.

Code:

select timeofday();

Output:

postgreSQL Timestamp 2JPG

  • The current timestamp function is used to select the current date and time in PostgreSQL. If we want to use the current date and time, then we need to use a current timestamp function in PostgreSQL.

Code:

select Current_timestamp;

Output:

postgreSQL Timestamp 3JPG

  • The timestamp is not a time zone data type; it only stores time and date; in some case, if the time zone of the server changed, it will not affect on actual data that we have stored into the database.
  • Timestamp data storage size is 8 bytes to store data into the database; timestamp data type is beneficial and important in PostgreSQL to store the data’s date and time data.
  • A low value of timestamp data type is 4713 BC, and a higher value of timestamp data type in PostgreSQL is 294276 AD.
  • The timestamp data type storage size is 8 byte.

Below example shows the size of timestamp data type in PostgreSQL:

Code:

SELECT typname as "datatype", typlen as "length of datatype" FROM pg_type WHERE typname like 'timestamp%';

Output:

postgreSQL Timestamp 4JPG

Examples of PostgreSQL Timestamp

Given below are the examples:

Example #1

We are giving data type of timestamp at the time of the creation of the table.

In the below example, we have given the timestamp format to the date of the joining column.

Code:

CREATE TABLE Employee_Timestamp (emp_id INT NOT NULL, emp_name character(10) NOT NULL, emp_address character(20) NOT NULL, emp_phone character(14), emp_salary INT NOT NULL, date_of_joining timestamp NOT NULL);

Output:

format to date of joining column

Example #2

The below example shows add a column and assign a timestamp data type to the newly added column using alter command are as follows.

Code:

ALTER TABLE Employee_Timestamp ADD COLUMN Date_Of_Resignation timestamp;

Output:

using alter command

Example #3

In the below example, we have used date using timestamp format are as follows. Using with and without time zone format.

  • With time zone: Below example shows timestamp with the time zone in PostgreSQL.

Code:

select timestampz ‘2020-03-18’;

Output:

With time zone

  • Without time zone: Below example shows timestamp without time zone in PostgreSQL is as follows.

Code:

select timestamp '2020-03-18';

Output:

postgreSQL Timestamp 9JPG

Conclusion

Timestamp uses 8 bytes storage space for storing data into the database; timestamp data type is beneficial in PostgreSQL to store the date and time data into the database. There are three types of timestamp data types in PostgreSQL, i.e. now (), timeofday () and Current_timestamp.

Recommended Articles

This is a guide to PostgreSQL Timestamp. Here we discuss the introductions, how to timestamp data type works in PostgreSQL? and examples. You may also have a look at the following articles to learn more –

  1. PostgreSQL Date Functions
  2. Date Function in PHP
  3. PostgreSQL Table
  4. PostgreSQL FETCH
  5. Guide to PostgreSQL UNIQUE Constraint
  6. PostgreSQL Notify | Examples

All in One Data Science Bundle (360+ Courses, 50+ projects)

360+ Online Courses

50+ projects

1500+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PostgreSQL Tutorial
  • Advanced
    • PostgreSQL Schema
    • Postgres List Schemas
    • PostgreSQL VARCHAR
    • Array in PostgreSQL
    • PostgreSQL DDL
    • PostgreSQL List Users
    • Postgres Default User
    • Postgres add user
    • PostgreSQL log_statement
    • PostgreSQL String Functions
    • PostgreSQL Compare Strings
    • PostgreSQL Text Search
    • PostgreSQL TEXT
    • PostgreSQL String Array
    • PostgreSQL Constraints
    • PostgreSQL UNIQUE Constraint
    • PostgreSQL INTERSECT
    • PostgreSQL Like
    • Cursors in PostgreSQL
    • PostgreSQL UNION ALL
    • Indexes in PostgreSQL
    • PostgreSQL Index Types
    • PostgreSQL REINDEX
    • PostgreSQL UNIQUE Index
    • PostgreSQL Clustered Index
    • PostgreSQL DROP INDEX
    • PostgreSQL DISTINCT
    • PostgreSQL FETCH
    • PostgreSQL RAISE EXCEPTION
    • PostgreSQL Auto Increment
    • Sequence in PostgreSQL
    • Wildcards in PostgreSQL
    • PostgreSQL Subquery
    • PostgreSQL Alias
    • PostgreSQL LIMIT
    • PostgreSQL Limit Offset
    • PostgreSQL LAG()
    • PostgreSQL Table
    • Postgres Show Tables
    • PostgreSQL Describe Table
    • PostgreSQL Lock Table
    • PostgreSQL ALTER TABLE
    • Postgres Rename Table
    • Postgres DROP Table
    • PostgreSQL Functions
    • PostgreSQL Math Functions
    • PostgreSQL Window Functions
    • Aggregate Functions in PostgreSQL
    • PostgreSQL Primary Key
    • Foreign Key in PostgreSQL
    • PostgreSQL Procedures
    • PostgreSQL Stored Procedures
    • PostgreSQL Views
    • PostgreSQL Materialized Views
    • Postgres Create View
    • PostgreSQL Triggers
    • PostgreSQL DROP TRIGGER
    • PostgreSQL Date Functions
    • PostgreSQL TO_DATE()
    • PostgreSQL Timestamp
    • PostgreSQL CURRENT_TIMESTAMP()
    • PostgreSQL Notify
    • PostgreSQL RANK()
    • PostgreSQL Select
    • PostgreSQL Average
    • PostgreSQL DATE_PART()
    • PostgreSQL EXECUTE
    • PostgreSQL COALESCE
    • PostgreSQL EXTRACT()
    • PostgreSQL Sort
    • PostgreSQL TO_CHAR
    • PostgreSQL Interval
    • PostgreSQL Number Types
    • PostgreSQL ROW_NUMBER
    • Alter Column in PostgreSQL
    • PostgreSQL Identity Column
    • PostgreSQL SPLIT_PART()
    • PostgreSQL CONCAT()
    • PostgreSQL replace
    • PostgreSQL TRIM()
    • PostgreSQL MAX
    • PostgreSQL DELETE
    • PostgreSQL Float
    • PostgreSQL OID
    • PostgreSQL log
    • PostgreSQL REGEXP_MATCHES()
    • PostgreSQL MD5 
    • PostgreSQL NOW()
    • PostgreSQL RANDOM
    • PostgreSQL round
    • PostgreSQL Trunc()
    • PostgreSQL TIME
    • PostgreSQL IS NULL
    • PostgreSQL CURRENT_TIME
    • PostgreSQL MOD()
    • Postgresql Count
    • PostgreSQL Datetime
    • PostgreSQL MIN()
    • PostgreSQL age()
    • PostgreSQL enum
    • PostgreSQL OR
    • PostgreSQL Wal
    • PostgreSQL NOT IN
    • PostgreSQL SET
    • PostgreSQL Current Date
    • PostgreSQL Compare Date
    • PostgreSQL SERIAL
    • PostgreSQL Database
    • PostgreSQL Clone Database
    • PostgreSQL Copy Database
    • PostgreSQL Restore Database
    • PostgreSQL DROP DATABASE
    • PostgreSQL ALTER DATABASE
    • Postgres DROP Database
    • Postgres Dump Database
    • PostgreSQL OFFSET
    • PostgreSQL GRANT
    • PostgreSQL COMMIT
    • PostgreSQL ROLLUP
    • PostgreSQL JSON
    • EXPLAIN ANALYZE in PostgreSQL
    • PostgreSQL Temporary Table
    • PostgreSQL cluster
    • PostgreSQL Replication
    • PostgreSQL Logical Replication
    • PostgreSQL flush privileges
    • PostgreSQL Tablespaces
    • CAST in PostgreSQL
    • PostgreSQL CTE
    • hstore in PostgreSQL
    • PostgreSQL DECODE()
    • PostgreSQL Vacuum
    • PostgreSQL EXCLUDE
    • Postgres Change Password
    • Postgres Delete Cascade
    • PostgreSQL EXCEPT
    • PostgreSQL Roles
    • PostgreSQL Link
    • PostgreSQL Partition
    • PostgreSQL column does not exist
    • PostgreSQL Log Queries
    • PostgreSQL escape single quote
    • PostgreSQL Query Optimization
    • PostgreSQL Character Varying
    • PostgreSQL Transaction
    • PostgreSQL Extensions
    • PostgreSQL Import CSV
    • PostgreSQL Client
    • PostgreSQL caching
    • PostgreSQL JDBC Driver
    • PostgreSQL Interview Questions
  • Basic
    • What is PostgreSQL
    • PostgreSQL Features
    • How to Install PostgreSQL
    • PostgreSQL Versions
    • PostgreSQL Architecture
    • PostgreSQL GUI
    • PostgreSQL Variables
    • PostgreSQL Data Types
    • PostgreSQL NOT NULL
    • PostgreSQL Integer
    • PostgreSQL Boolean
    • PostgreSQL NULLIF
    • PostgreSQL Administration
    • PostgreSQL Commands
    • PostgreSQL Operators
    • PostgreSQL IN Operator
  • Control Statement
    • PostgreSQL IF Statement
    • PostgreSQL if else
    • PostgreSQL CASE Statement
    • PostgreSQL LOOP
    • PostgreSQL For Loop
    • PostgreSQL While Loop
  • Joins
    • Joins in PostgreSQL
    • PostgreSQL Inner Join
    • PostgreSQL Outer Join
    • LEFT OUTER JOIN in PostgreSQL
    • PostgreSQL FULL OUTER JOIN
    • PostgreSQL LEFT JOIN
    • PostgreSQL Full Join
    • PostgreSQL Cross Join
    • PostgreSQL NATURAL JOIN
    • PostgreSQL UPDATE JOIN
  • Queries
    • PostgreSQL Queries
    • PostgreSQL WHERE Clause
    • PostgreSQL WITH Clause
    • PostgreSQL ORDER BY
    • PostgreSQL ORDER BY Random
    • PostgreSQL GROUP BY
    • PostgreSQL group_concat
    • PostgreSQL HAVING
    • PostgreSQL Recursive Query
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • 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

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

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

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

EDUCBA

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

Forgot Password?

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

Special Offer - All in One Data Science Bundle (360+ Courses, 50+ projects) Learn More