65

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

49

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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')
I love reading these types of questions and answers, thanks

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.