What is the best PostgreSQL data type for year data, e.g., 2006 or 1847.
TEXT, SMALLINT, DATE? Ideally, I’d like to be able to query that column with a second year-month-day column (in DATE format).
What is the best PostgreSQL data type for year data, e.g., 2006 or 1847.
Share
A year is an integer and even supports integer arithmetic in a meaningful way so
textmakes no sense. You don’t have a month or day sodateis right out the window. The fine manual has this to say aboutsmallint:That leaves
integeras a natural choice. If you’re planning to combine this value withdates andtimestamps, thenintervalmight make sense as the date and time functions and operators have a good understanding ofintervals.So it depends on what you’re planning on doing with this “year” and what it really is. Sounds like a simple
integeris appropriate but you might have to muck around a bit to combine it with your “second year-month-day column” (adatecolumn presumably) depending on how they need to work together.