I’m using postgresql and python and I need to store data group by week of the year. So, there’s plenty alternatives:
- week and year in two separated fields
- a date pointing to the start of the week (or a random day of the week)
- And, the one I like: an interval type.
I never use it, but reading the docs, seems to fit. But then, reading psycopg docs I found interval mapped to python timedelta object… seems weird to me, a timedelta is just a difference.
So, there are two question here, really:
- Can I handle this choice using psycopg2?
- Is the better alternative?
Thanks
The PostgreSQL interval type isn’t really what you’re looking for — it’s specifically intended for storing an arbitrary length of time, ranging anywhere from a microsecond to a few million years. An interval has no starting or ending point; it’s just a measure of “how long”.
If you’re specifically after storing which week an event is associated with, you’re probably better off with either of your first two options.