<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Timur</title>
    <description>The latest articles on Forem by Timur (@brainstorm86).</description>
    <link>https://forem.com/brainstorm86</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3630476%2F1a49f3ad-c9ea-47bd-84aa-e437f7225271.png</url>
      <title>Forem: Timur</title>
      <link>https://forem.com/brainstorm86</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/brainstorm86"/>
    <language>en</language>
    <item>
      <title>How have I managed Date types in my asyncio python web scraper using PostgreSQL?</title>
      <dc:creator>Timur</dc:creator>
      <pubDate>Thu, 27 Nov 2025 15:20:57 +0000</pubDate>
      <link>https://forem.com/brainstorm86/how-have-i-managed-date-types-in-my-asyncio-python-web-scraper-using-postgresql-2272</link>
      <guid>https://forem.com/brainstorm86/how-have-i-managed-date-types-in-my-asyncio-python-web-scraper-using-postgresql-2272</guid>
      <description>&lt;p&gt;*&lt;em&gt;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. &lt;br&gt;
Firstly, let us cover the basics. *&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. A recommended format of data storage in PostgreSQL&lt;/strong&gt;&lt;br&gt;
The standard SQL convention is to use &lt;a href="https://en.wikipedia.org/wiki/ISO_8601" rel="noopener noreferrer"&gt;ISO 8601&lt;/a&gt; format for date types but there are a lot of other &lt;a href="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3run5bjj6n68xnmdbbm8.png" rel="noopener noreferrer"&gt;formats&lt;/a&gt; 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.&lt;br&gt;
&lt;a href="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bg238jme7l6znezatwji.png" rel="noopener noreferrer"&gt;table from docs&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. In which timezone should you store data?&lt;/strong&gt;&lt;br&gt;
For convinience, you must convert all your data to &lt;a href="https://en.wikipedia.org/wiki/Coordinated_Universal_Time" rel="noopener noreferrer"&gt;UTC&lt;/a&gt; , before making input into the database. It is used as an international , universal unit of measurement. &lt;br&gt;
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.&lt;br&gt;
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:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def datetime_utc_converter(datetime_obj: datetime) -&amp;gt; datetime:
    return datetime_obj.astimezone(tz=timezone.utc)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this approach, I am sure my data is managed in a correct, non-ambiguous way.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. How PostgreSQL client (pgadmin in my example) show your data?&lt;/strong&gt;&lt;br&gt;
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. &lt;br&gt;
For the default timezone, PostgreSQL uses the default parameter specified in postgresql.conf. You may see its location by running this query in psql:&lt;br&gt;
&lt;code&gt;SHOW config_file&lt;/code&gt;&lt;br&gt;
Then you may change the default value. If you want to see already set parameter, then use:&lt;br&gt;
&lt;code&gt;SHOW timezone&lt;/code&gt;&lt;br&gt;
You may see available timezones by: &lt;br&gt;
&lt;code&gt;SELECT * FROM pg_timezone_names&lt;/code&gt;&lt;br&gt;
And set one of them by:&lt;br&gt;
e.g SET 'America/Araguaina' choose one that you need.&lt;br&gt;
It is recommended to set your default timezone to UTC.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Overall, hope you enjoyed my small article, the whole information was taken from PostgreSQL docs &lt;a href="https://www.postgresql.org/docs/18/datatype-datetime.html" rel="noopener noreferrer"&gt;Chapter 8.5. Date/Time Types&lt;/a&gt; and there a useful video of &lt;a href="https://youtu.be/oWF5jTFBSHw" rel="noopener noreferrer"&gt;Aaron Francis&lt;/a&gt; about the topic. &lt;br&gt;
I look forward to all your questions and additions, and please correct me if I am wrong somewhere!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>postgres</category>
      <category>tutorial</category>
      <category>database</category>
    </item>
  </channel>
</rss>
