agora inbox for pgsql-sql@postgresql.org  
help / color / mirror / Atom feed
From: Martin Edlman <martin.edlman@gmail.com>
To: pgsql-sql@lists.postgresql.org
Cc: Dmitry Igrishin <dmitigr@gmail.com>
Cc: stan <stanb@panix.com>
Subject: Re: type for storing emails?
Date: Tue, 12 Nov 2019 08:37:29 +0100
Message-ID: <ebd4974b-4102-51cc-f1c6-e5fb81ee4faa@gmail.com> (raw)
In-Reply-To: <CAAfz9KMASGyv9eToPJ15phRqaiQwmes4KNU9AdS9Ge53+hmW3w@mail.gmail.com>
References: <20191111175920.GA16283@panix.com>
	<CAAfz9KMASGyv9eToPJ15phRqaiQwmes4KNU9AdS9Ge53+hmW3w@mail.gmail.com>

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@email');
ERROR:  value for domain email_address violates check constraint 
"email_address_check"
 > insert into tbl (email) values ('correct@email.dot.domain');
Query returned successfully: one row affected

 > select 'bad@email'::email_address;
ERROR:  value for domain email_address violates check constraint 
"email_address_check"
 > select 'correct@email.dot.domain'::email_address
correct@email.dot.domain

Regards, Martin

> https://github.com/petere/pgemailaddr
> 
> On Mon, 11 Nov 2019, 20:59 stan, <stanb@panix.com <mailto:stanb@panix.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
> 
> 






view thread (7+ messages)  latest in thread

Message-ID: <ebd4974b-4102-51cc-f1c6-e5fb81ee4faa@gmail.com>
Permalink:  ../ebd4974b-4102-51cc-f1c6-e5fb81ee4faa@gmail.com/
Also on:    postgresql.org/message-id/ebd4974b-4102-51cc-f1c6-e5fb81ee4faa@gmail.com

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: martin.edlman@gmail.com, pgsql-sql@lists.postgresql.org, dmitigr@gmail.com, stanb@panix.com
  Subject: Re: type for storing emails?
  In-Reply-To: <ebd4974b-4102-51cc-f1c6-e5fb81ee4faa@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox