Skip to content

Commit c6bb8df

Browse files
committed
Add Storing Emails With citext as a postgres til
1 parent e16c130 commit c6bb8df

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ variety of languages and technologies. These are things that don't really
77
warrant a full blog post. These are mostly things I learn by pairing with
88
smart people at [Hashrocket](http://hashrocket.com/).
99

10-
_376 TILs and counting..._
10+
_377 TILs and counting..._
1111

1212
---
1313

@@ -206,6 +206,7 @@ _376 TILs and counting..._
206206
- [Sets With The Values Command](postgres/sets-with-the-values-command.md)
207207
- [Sleeping](postgres/sleeping.md)
208208
- [Special Math Operators](postgres/special-math-operators.md)
209+
- [Storing Emails With citext](postgres/storing-emails-with-citext.md)
209210
- [String Contains Another String](postgres/string-contains-another-string.md)
210211
- [Temporarily Disable Triggers](postgres/temporarily-disable-triggers.md)
211212
- [Temporary Tables](postgres/temporary-tables.md)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Storing Emails With citext
2+
3+
Email addresses should be treated as case-insensitive because they are. If a
4+
user is trying to sign in with their email address, we shouldn't care if
5+
they type `[email protected]` or `[email protected]`. Both of those email
6+
addresses should be treated as equal and ultimately lead us to the same
7+
`User` record.
8+
9+
With the
10+
[`citext`](http://www.postgresql.org/docs/current/static/citext.html)
11+
extension, we can create a column that acts as a case-insensitive text type.
12+
Any comparisons on a column of that type will internally have the `lower`
13+
function executed on the arguments.
14+
15+
The following example shows this in action:
16+
17+
```sql
18+
create extension if not exists citext;
19+
20+
create table citext_emails (
21+
id serial primary key,
22+
email citext not null unique
23+
);
24+
25+
insert into citext_emails (email) values ('[email protected]');
26+
27+
select * from citext_emails where email = '[email protected]';
28+
-- id | email
29+
-- ----+------------------
30+
31+
```
32+
33+
See
34+
[`citext-emails.sql`](https://github.com/jbranchaud/postgresing/blob/master/citext-emails.sql)
35+
for a full example.

0 commit comments

Comments
 (0)