Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datatype for a URL in PostgreSQL

I need to store a URL in a PostgreSQL table. What is the best datatype for a field that will hold a URL with an undetermined length?

Thanks in advance.

like image 928
Jeena Rita Avatar asked Jan 13 '17 11:01

Jeena Rita


People also ask

What is the datatype for URL?

You should use a VARCHAR with an ASCII character encoding. URLs are percent encoded and international domain names use punycode so ASCII is enough to store them.

What is integer [] in PostgreSQL?

Integer ( INT ) is a 4-byte integer that has a range from -2,147,483,648 to 2,147,483,647. Serial is the same as integer except that PostgreSQL will automatically generate and populate values into the SERIAL column. This is similar to AUTO_INCREMENT column in MySQL or AUTOINCREMENT column in SQLite.

What is text data type in PostgreSQL?

PostgreSQL supports a character data type called TEXT. This data type is used to store character of unlimited length. It is represented as text in PostgreSQL. The performance of the varchar (without n) and text are the same. Syntax: variable_name TEXT.


1 Answers

The answer depends on what you intend to do with the data.

If you just need to store some uris in order to print them when requested, the text datatype seems indicated. There seems to be no standard about the maximum length of an url (note that browsers have their own limits, for example at least some years ago IE was limited to 2083 characters, but this is unrelated to our problem).

If you need some advanced operations on uris (for example, computing the base uri or extract some other parts), then you may want to use some libraries designed for this purpose. One example of such library (actually I know of no alternative) is pguri.

like image 157
Fabian Pijcke Avatar answered Oct 03 '22 06:10

Fabian Pijcke