52

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.

1 Answer 1

34

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.

2
  • 6
    Nice library you got there, I wasn't aware of that. The built-in FTS has some URI parsing facilities, but it's quite cumbersome to use it directly. -- i.e. select * from ts_debug('http://example.com:8080/foo/bar?baz=1')
    – pozs
    Jan 13, 2017 at 15:22
  • 1
    Yes but your solution has the advantage of using no external library :p Jan 13, 2017 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.