I'm migrating my MySQL database to PostgreSQL and have a simple question:
What is the best equivalent of varchar(30)
in PostgreSQL? Is it text
?
Asked
Active
Viewed 1.5k times
7

Erwin Brandstetter
- 605,456
- 145
- 1,078
- 1,228

EdwardBloom
- 99
- 1
- 1
- 4
-
1http://www.postgresql.org/docs/9.1/static/datatype-character.html – Kuberchaun May 07 '14 at 19:18
-
Yes, generally that is what you would do unless you have a very specific data type you are pushing and you want to store it specifically for say a path. – VikingBlooded May 07 '14 at 19:20
-
Also consider: http://stackoverflow.com/questions/20326892/any-downsides-of-using-data-type-text-for-storing-strings – Erwin Brandstetter May 07 '14 at 20:44
4 Answers
8
According to PostgreSQL documentation you can use the following data types:
Of the above the only type that is unlimited is text
. So, if you need unlimited space then use text
. However, if you know how large the field can be I would use varchar(n)
. There is no point in using an unlimited data type for a finite requirement. By doing so, you are just wasting space.

Linger
- 14,942
- 23
- 52
- 79
-
1Actually, you don't waste any space - unless you store more than 30 characters. – ypercubeᵀᴹ May 07 '14 at 20:56
-
@ypercube, I agree. If you allow someone the opportunity to store more information then they usually will. The whole point of setting the length is to control how much the users can input. Users will corrupt a database that is left to open. That was my point. I didn't spell it out well enough. – Linger May 07 '14 at 20:59
8
The best mapping to varchar(30) in MySQL is varchar(30) in PostgreSQL. varchar is part of the sql standard and can be used as is in postgresql.
TEXT is non standard, since you are in a migration situation it might be best to stick to standard elements.

tburette
- 181
- 5
1
here is the documentation
http://www.postgresql.org/docs/8.4/static/datatype.html
you can also use vachar(n)

Papouche Guinslyzinho
- 5,277
- 14
- 58
- 101