![]() |
VOOZH | about |
For processing timestamps, PostgreSQL supports two data types timestamp and timestamptz. timestamp datatype helps us create a timestamp without timezone, and timestamptz helps us create a timestamp with a timezone. One may store both date and time with the timestamp datatype. It does not, however, include time zone information. It implies that if you alter your database server's timezone, the timestamp value saved in the database will not automatically update, in these situations timestamptz datatype is used.
The below code is an example of the datatypes. psycopg2.connect() method is used to establish a connection to the database. The cursor is created using the connection.cursor() method. execute() method executes the sql commands given. A table named timestamp_data is created. A string of the formatted timestamp is inserted in the table created. Values are fetched from the table. In the table, we can see column timestamp_timezone also shows timezone.
Output:
👁 ImageIn this example, psycopg2 and DateTime packages are imported. psycopg2.connect() method is used to establish a connection to the database. The cursor is created using the connection.cursor() method. execute() method executes the SQL commands given. Values are inserted in the table created. datetime.now() is used to calculate the current_timestamp, it's further inserted into the table. cursor.fetchall() method is used to fetch all the rows.
Output:
👁 Image(Decimal('4565.89'), datetime.datetime(2022, 3, 6, 19, 2, 3, 669114,
tzinfo=datetime.timezone(datetime.timedelta(seconds=19800))))