PostgreSql storing a value as integer vs varchar PostgreSql storing a value as integer vs varchar postgresql postgresql

PostgreSql storing a value as integer vs varchar


In terms of space, a bigint takes 8 bytes while a 15-character string will take up to 19 bytes (a 4 bytes header and up to another 15 bytes for the data), depending on its value. Also, generally speaking, equality checks should be slightly faster for numerical operations. Other than that, it widely depends on what you're intending to do with this data. If you intend to use numerical/mathematical operations or query according to ranges, you should use a bigint.


You can store them as BIGINT as the comparison using the INT are faster when compared with varchar. I would also advise to create an index on the column if you are expecting millions of records to make the data retrieval faster.

You can also check this forum:

Indexing is fastest on integer types. Char types are probably stored underneath as integers (guessing.) VARCHARs are variable allocation and when you change the string length it may have to reallocate.

Generally integers are fastest, then single characters then CHAR[x] types and then VARCHARS.