Re: type for storing emails?

From: Martin Edlman <martin(dot)edlman(at)gmail(dot)com>
To: pgsql-sql(at)lists(dot)postgresql(dot)org
Cc: Dmitry Igrishin <dmitigr(at)gmail(dot)com>, stan <stanb(at)panix(dot)com>
Subject: Re: type for storing emails?
Date: 2019-11-12 07:37:29
Message-ID: ebd4974b-4102-51cc-f1c6-e5fb81ee4faa@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

I use domains for such types (email, url, zip code, phone, ...). Using the
regexp constraint you can validate the value. I took the regexp from some
web site, you can change it if you wish.

CREATE DOMAIN email_address
AS character varying(100)
COLLATE pg_catalog."default"
CONSTRAINT email_address_check CHECK (VALUE::text ~*
'^[-+_\.a-z0-9]+@([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z0-9]+(-[a-z0-9]+)*$'::text
OR VALUE::text = ''::text);

> create table tbl (id serial, email email_address);
> insert into tbl (email) values ('bad(at)email');
ERROR: value for domain email_address violates check constraint
"email_address_check"
> insert into tbl (email) values ('correct(at)email(dot)dot(dot)domain');
Query returned successfully: one row affected

> select 'bad(at)email'::email_address;
ERROR: value for domain email_address violates check constraint
"email_address_check"
> select 'correct(at)email(dot)dot(dot)domain'::email_address
correct(at)email(dot)dot(dot)domain

Regards, Martin

> https://github.com/petere/pgemailaddr
>
> On Mon, 11 Nov 2019, 20:59 stan, <stanb(at)panix(dot)com <mailto:stanb(at)panix(dot)com>>
> wrote:
>
> Does anyone have a type they have developed for storing emails. I need
> to do that, and the things that are in my thoughts on this are storing
> it as
> a derived type of citext, as case should not matter, and enforcing the at
> sign with pretty much anything on the left side of it, and something that
> looks like a domain on the right side of it.
>
>
> --
> "They that would give up essential liberty for temporary safety deserve
> neither liberty nor safety."
>                                                 -- Benjamin Franklin
>
>

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message stan 2019-11-12 15:07:47 FW: Re: type for storing emails?
Previous Message Rene Romero Benavides 2019-11-11 18:29:03 Re: type for storing emails?