I am using Django with a Postgres backend.
In the Django models, I have to set the max_length attribute on the Charfields, then the Django ORM deals with the database in the backend. In a lot of cases, those are rather small fields (e.g. say names, or city names etc.) that I don't expect would be very long. That application deals with existing data that isn't particularly cleaned. So I don't want to set something like
name = models.Charfield(max_length=50, ...)
Just to realize that someone triple wrote his name in that field and that the field is 75 characters long. On the other hand, I don't want to set huge values and needlessly increase database size just to save me a little bit of trouble.
My current thinking is that Postgres probably reserves some minimal amount of space (e.g. likely that max_length=10 and max_length=15 both take the same storage), so I would set the max_length to that minimal size (or low multiple thereof).
What do you recommend and why?
VARCHAR
values only occupy as much space as the actual strings they contain (plus length, typically two bytes). So, if Django mapsCharfield
toVARCHAR
, no space is wasted. Also, this.varchar
ortext
- Postgres only stores as many characters as you put into the columns. You should see the length limit as a business constraint, rather than a technical restriction.text
. See: dba.stackexchange.com/q/125499/3684, dba.stackexchange.com/a/21496/3684, dba.stackexchange.com/a/89433/3684