7

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?

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
EdwardBloom
  • 99
  • 1
  • 1
  • 4

4 Answers4

8

According to PostgreSQL documentation you can use the following data types:

enter image description here

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
  • 1
    Actually, 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

Go ahead and make use of VARCHAR(30).

Kuberchaun
  • 29,160
  • 7
  • 51
  • 59
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