CHAR(n) Vs VARCHAR(N) Vs Text In Postgres

IT Support Forum Forums Databases PostgreSQL General Discussion CHAR(n) Vs VARCHAR(N) Vs Text In Postgres

Viewing 0 reply threads
  • Author
    Posts
    • #2342
      Webmaster
      Keymaster

      Before I get into what is the difference between CHAR(n), VARCHAR(n) and Text data types in PostgreSQL, I’ll give you the short answer to what you want to know:

      If you want to store some text with an unknown length, use the Text data type.
      If you want to store some text with an unknown length, but you know the maximum length, use VARCHAR(n).
      If you want to store some text with a known exact length, use CHAR(N).

      There are some variations on that, but that will fit most scenarios.

      Basically, VARCHAR(n) stores the text in the database as is, CHAR(n) pads the data out with blank space so it’s as long as the maximum size. Both are exactly as performant as each other, but some queries might take longer if you’re doing a text comparison (because you’re comparing more data in the padded CHAR(n) data).

      I won’t go into how the Text data type works, but basically it has no maximum size and overflows the excess data into a Toast table in Postgres. So use VARCHAR(n) or CHAR(n) if you can.

Viewing 0 reply threads
  • You must be logged in to reply to this topic.