Let's assume I want to store phone numbers in a database. I may accept phone numbers from outside of the United States. How would I go about storing these phone numbers?
PostgreSQL – How to Store Phone Numbers Correctly
data integritydata validationdatatypespostgresql
Related Question
- MySQL – Best Practices for Storing Phone Numbers
- MySQL – Best Datatype for Phone Numbers: VARCHAR, INT, or BIGINT?
- PostgreSQL – How to Store HTTP Response
- Postgresql – Phone number with different formats lookup
- PostgreSQL – How to Remove Numbers Before First Comma
- Postgresql – How do i store hash indices on the database
Best Answer
libphonenumber
When possible always use the canonical form. The more normalized the form the better. If there is a standard, use it. For this problem, let's use Google's libphonenumber, by proxy of pg-libphonenumber.
This currently installs the
phone_number
type which has comparison operators and functions. It stores the number in an international canonical form. This is the best compromise in my opinion.Because we can tell when phone numbers equal each other and we provide an internal normal form, we can do this..
(returns true). This also means that
DISTINCT
works so we can do this to get the effect you seem to want above.That puts in..