Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iUQk8-00018u-5p for pgsql-sql@arkaria.postgresql.org; Tue, 12 Nov 2019 07:37:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iUQk6-0003y0-N0 for pgsql-sql@arkaria.postgresql.org; Tue, 12 Nov 2019 07:37:38 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iUQk6-0003xt-BM for pgsql-sql@lists.postgresql.org; Tue, 12 Nov 2019 07:37:38 +0000 Received: from mail.bezdrat.net ([213.250.192.16]) by makus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1iUQk3-0001qG-QR for pgsql-sql@lists.postgresql.org; Tue, 12 Nov 2019 07:37:37 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.bezdrat.net (Postfix) with ESMTP id 7C2EE25A; Tue, 12 Nov 2019 08:37:33 +0100 (CET) X-Virus-Scanned: amavisd-new at fortech.cz Received: from mail.bezdrat.net ([127.0.0.1]) by localhost (mail.bezdrat.net [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id KZ4_W5k36svx; Tue, 12 Nov 2019 08:37:31 +0100 (CET) Received: from ntb-edas.anycode.eu (edas.lit.cz [213.250.198.38]) (Authenticated sender: edlman@bezdrat.net) by mail.bezdrat.net (Postfix) with ESMTPSA id 91F73105; Tue, 12 Nov 2019 08:37:29 +0100 (CET) Subject: Re: type for storing emails? To: pgsql-sql@lists.postgresql.org Cc: Dmitry Igrishin , stan References: <20191111175920.GA16283@panix.com> From: Martin Edlman Message-ID: Date: Tue, 12 Nov 2019 08:37:29 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.2.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: cs Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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, > > 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 > >