In PostgreSQL, the timestamp data type is used to store both date and time. It is crucial for applications that require precise time tracking.
The timestamp with time zone (timestamptz) ensures that the timestamp is stored in UTC and displayed in the client’s local time.
A timestamp without time zone (timestamp) does not consider time zones, storing the date and time as they are.
CREATE TABLE events (
event_id SERIAL PRIMARY KEY,
event_name VARCHAR(100),
event_time TIMESTAMP WITH TIME ZONE
);
Use the CURRENT_TIMESTAMP function to get the current date and time.
Extract specific parts of a timestamp using the EXTRACT function.
SELECT event_name, EXTRACT(YEAR FROM event_time) AS event_year
FROM events;
Console Output:
event_name | event_year
-----------------------
Conference | 2023
You can set the time zone for a session using the SET TIME ZONE command.
Convert a timestamp to a different time zone using the AT TIME ZONE clause.
SET TIME ZONE 'America/New_York';
SELECT event_name, event_time AT TIME ZONE 'UTC' AS utc_time
FROM events;
PostgreSQL supports various time zone abbreviations, which can be used interchangeably with full names.
SELECT now() AT TIME ZONE 'EST';
Console Output:
2023-10-29 15:00:00
You can add or subtract intervals to/from timestamps to calculate future or past dates.
The interval data type represents a period of time and can be used in arithmetic operations with timestamps.
SELECT now() + INTERVAL '1 day' AS tomorrow;
Calculate the difference between two timestamps using the subtraction operator.
SELECT event_name, event_time - CURRENT_TIMESTAMP AS time_until_event
FROM events;
Console Output:
time_until_event
----------------
2 days 3 hours
Use the TO_CHAR function to format timestamps into a more readable string format.
SELECT TO_CHAR(now(), 'YYYY-MM-DD HH24:MI:SS') AS formatted_time;
Customize the output format by specifying a pattern in the TO_CHAR function.
SELECT TO_CHAR(now(), 'Day, DD Month YYYY') AS custom_format;
Console Output:
formatted_time
----------------
2023-10-29 15:00:00
Use the COALESCE function to provide a default value for null timestamps.
SELECT COALESCE(event_time, '2023-01-01') AS event_time
FROM events;
Ensure arithmetic operations account for null values to avoid errors.
SELECT event_name, COALESCE(event_time - CURRENT_TIMESTAMP, INTERVAL '0') AS time_until_event
FROM events;
Console Output:
event_time
-----------
2023-01-01
Use standard comparison operators such as =, >, and < to compare timestamps.
SELECT event_name FROM events
WHERE event_time > CURRENT_TIMESTAMP;
The BETWEEN operator is useful for checking if a timestamp falls within a specific range.
SELECT event_name FROM events
WHERE event_time BETWEEN '2023-01-01' AND '2023-12-31';
Console Output:
event_name
-----------
Conference
Indexing a timestamp column can significantly improve query performance, especially for large datasets.
CREATE INDEX idx_event_time ON events(event_time);
Proper indexing can reduce the execution time of queries involving timestamp comparisons.
EXPLAIN ANALYZE SELECT event_name FROM events WHERE event_time > CURRENT_TIMESTAMP;
Console Output:
QUERY PLAN
-----------------
Index Scan using idx_event_time on events
PostgreSQL allows specifying the precision of fractional seconds for timestamps, enhancing accuracy.
CREATE TABLE precise_events (
event_id SERIAL PRIMARY KEY,
event_time TIMESTAMP(3)
);
The default precision is microseconds, but you can adjust it to meet specific requirements.
INSERT INTO precise_events (event_time) VALUES ('2023-10-29 15:00:00.123');
Console Output:
event_time
--------------------
2023-10-29 15:00:00.123
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies