*Hi there! It is my first post here so today I'll be talking about Date types managemnt in PostgreSQL, as example it will be my own web scraper.
Firstly, let us cover the basics. *
1. A recommended format of data storage in PostgreSQL
The standard SQL convention is to use ISO 8601 format for date types but there are a lot of other formats that can be stored in PostgreSQL , by the way, PostgreSQL provides a lot of convenient features for managing date types, especially, that is why I have moved my scraper from SQLite to PostgreSQL. If you deal with timestamptz (see box) , basically , in Python it is something similar to datetime obj. You must explicitly specify wether it is with or without timezone, because PostgreSQL never examines the content of a literal string before determining its type. You must pay attention, if you are using some dates in YMD related formats as it would be more memory efficient to use a "Date" type for them , despite of timestamptz.
table from docs
2. In which timezone should you store data?
For convinience, you must convert all your data to UTC , before making input into the database. It is used as an international , universal unit of measurement.
Imagine the situation: you scrape two websites with different timezones -one has European, the other American. Of course, you can specify an offset and maybe a specific timezone, but why bother when you can just store them in UTC? Therefore, if a user is from the USA, you should specify their local timezone when outputting the timestamptz data.
In my example, first, I converted the raw string into a Python datetime object and defined the timezone offset (e.g., 2025-11-27 09:36:32+01:00). Second, I converted it to UTC. Here is the relevant code:
def datetime_utc_converter(datetime_obj: datetime) -> datetime:
return datetime_obj.astimezone(tz=timezone.utc)
With this approach, I am sure my data is managed in a correct, non-ambiguous way.
3. How PostgreSQL client (pgadmin in my example) show your data?
This can be one of the most confusing parts in today's article for beginners: you may insert data in UTC, but the data will be shown in your local or the specified timezone.
For the default timezone, PostgreSQL uses the default parameter specified in postgresql.conf. You may see its location by running this query in psql:
SHOW config_file
Then you may change the default value. If you want to see already set parameter, then use:
SHOW timezone
You may see available timezones by:
SELECT * FROM pg_timezone_names
And set one of them by:
e.g SET 'America/Araguaina' choose one that you need.
It is recommended to set your default timezone to UTC.
Overall, hope you enjoyed my small article, the whole information was taken from PostgreSQL docs Chapter 8.5. Date/Time Types and there a useful video of Aaron Francis about the topic.
I look forward to all your questions and additions, and please correct me if I am wrong somewhere!
Top comments (0)