PostgreSQL Tutorial
Data Types
Querying & Filtering Data
Managing Tables
Modifying Data
Conditionals
Control Flow
Transactions & Constraints
Working with JOINS & Schemas
Roles & Permissions
Working with Sets
Subquery & CTEs
User-defined Functions
Important In-Built Functions
PostgreSQL PL/pgSQL
Variables & Constants
Stored Procedures
Working with Triggers
Working with Views & Indexes
Errors & Exception Handling
In PostgreSQL, the TIMESTAMP
data type is used to store a specific date and time. It can represent both a date (year, month, day) and time (hour, minute, second, and fractional seconds).
Here's a deeper look at the TIMESTAMP
data type:
Representation:
TIMESTAMP
type combines both date and time into a single value. This is distinct from having separate DATE
and TIME
data types.Syntax:
When defining a column with the TIMESTAMP
data type:
CREATE TABLE events ( event_name TEXT, event_datetime TIMESTAMP );
Precision:
TIMESTAMP(p)
, where p
is the number of fractional digits for the seconds field.TIMESTAMP(3)
would store the value with millisecond precision.TIMESTAMP with Time Zone:
TIMESTAMP WITH TIME ZONE
variant, often abbreviated as TIMESTAMPTZ
.Input Formats:
You can provide TIMESTAMP
values in various string formats:
INSERT INTO events (event_name, event_datetime) VALUES ('Conference', '2023-09-12 14:00:00');
Range:
TIMESTAMP
type in PostgreSQL can represent dates from 4713 BC to 294276 AD.Functions and Operations:
TIMESTAMP
values. Examples include date_trunc()
, age()
, and interval arithmetic.TIMESTAMP
using the EXTRACT
function.Storage:
TIMESTAMP
without time zone requires 8 bytes of storage.TIMESTAMP
with time zone also requires 8 bytes (despite the potentially confusing name, the time zone information isn't stored, just the adjusted UTC time).Considerations:
In summary, the TIMESTAMP
data type in PostgreSQL is a versatile way to represent points in time, including both the date and time of day. It offers precision and a range suitable for a wide variety of applications, but developers should be cautious and considerate when working with time zones.
PostgreSQL TIMESTAMP data type example: The TIMESTAMP data type in PostgreSQL is used to store date and time values. Here's an example of creating a table with a TIMESTAMP column:
CREATE TABLE example_table ( id SERIAL PRIMARY KEY, event_timestamp TIMESTAMP );
How to use TIMESTAMP data type in PostgreSQL: Use the TIMESTAMP data type to store date and time values:
CREATE TABLE example_table ( id SERIAL PRIMARY KEY, event_timestamp TIMESTAMP );
TIMESTAMP data type operations in PostgreSQL: Perform operations on TIMESTAMP data, such as addition and subtraction:
SELECT current_timestamp + interval '1 day' AS tomorrow;
Extracting components from TIMESTAMP in PostgreSQL: Extract components like year, month, day, hour, minute, and second from a TIMESTAMP value:
SELECT EXTRACT(YEAR FROM event_timestamp) AS year FROM example_table;
Formatting TIMESTAMP output in PostgreSQL:
Format TIMESTAMP output using the TO_CHAR
function:
SELECT TO_CHAR(event_timestamp, 'YYYY-MM-DD HH24:MI:SS') AS formatted_timestamp FROM example_table;
Convert string to TIMESTAMP in PostgreSQL:
Convert a string to TIMESTAMP using the ::
cast:
SELECT '2023-01-01 12:00:00'::TIMESTAMP AS converted_timestamp;
PostgreSQL create table with TIMESTAMP column: Create a table with a TIMESTAMP column:
CREATE TABLE example_table ( id SERIAL PRIMARY KEY, event_timestamp TIMESTAMP );