Skip to content

Instantly share code, notes, and snippets.

@bjeanes
Created April 7, 2016 01:44
Show Gist options
  • Select an option

  • Save bjeanes/d51e111f6c004f0b6ec05971ac1b86ce to your computer and use it in GitHub Desktop.

Select an option

Save bjeanes/d51e111f6c004f0b6ec05971ac1b86ce to your computer and use it in GitHub Desktop.
Email domain type for postgres
CREATE EXTENSION IF NOT EXISTS citext;
-- http://www.rfc-editor.org/errata_search.php?rfc=3696&eid=1690
CREATE DOMAIN email AS citext
CHECK (
VALUE IS NULL OR (
-- is not more than 254 chars
length(VALUE) <= 254 AND
-- vaguely looks like an email address
VALUE ~ '.@.' AND
-- local part is not more than 64 chars
length(split_part(VALUE, '@', 1)) <= 64 AND
-- domain part is not more than 255 chars
length(split_part(VALUE, '@', 2)) <= 255
)
);
@bjeanes
Copy link
Author

bjeanes commented Apr 7, 2016

Field type allows for null values because the column in a table can still be NOT NULL if need be.

@bjeanes
Copy link
Author

bjeanes commented Jun 1, 2016

https://github.com/petere/pgemailaddr would be even better, but not currently available on Heroku's Postgres offering.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment