Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
PostgresDoc: Using Postgres as a Document Orien...
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Myles Braithwaite
September 13, 2016
Technology
220
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
PostgresDoc: Using Postgres as a Document Oriented Database
Myles Braithwaite
September 13, 2016
More Decks by Myles Braithwaite
See All by Myles Braithwaite
pgloader
myles
0
84
IndieWeb - The person-focused alternative to the corporate web
myles
0
190
Let's Encrypt All the Things
myles
1
130
Chat Bots
myles
1
170
Ansible: Orchestrate Your Infustrature Like Gustav Mahler
myles
0
220
Take a Stroll in the Bazaar
myles
0
150
LessCSS
myles
0
52
jrnl
myles
0
52
Apache CouchDB
myles
0
47
Other Decks in Technology
See All in Technology
Reactの設計論
uhyo
13
6.3k
AIとペアプロを始める。人とのペアプロをやめる。ペアプロの良さを改めて知る。もっと好きになった。 / Rediscovering Pair Programming
honyanya
1
330
Adaptive Warehouse を今すぐ導入すべき理由と迷ったときの判断基準
__allllllllez__
0
140
omasushiというライブラリを作った
polidog
PRO
0
180
Webとヘルスデータ
yukukotani
1
200
コーディングエージェントでM5Stack系の開発を少し試した時の話 / M5 Japan Tour 2026 Autumn 東京
you
PRO
0
110
2026-09-09 【sigma_ucj#1】Sigma を IaC 管理したい! / IaC for Sigma
civitaspo
0
100
Deploying a Full-Stack Bun-Native Framework on Cloudflare Workers
7nohe
0
120
AI-DLCって実際どう? 〜聞きたいこと全部聞いてみる〜
news_it_enj
0
250
TinyGo 開発サイクルを高速化する:Go で作るエミュレータ入門
zozotech
PRO
1
390
作品が生態系になった ─ Mini Tokyo 3D から世界へ
nagix
0
150
GuardDuty 検知対応を DevOps Agent で効率化しようとしている話 / GuardDuty Investigations with DevOps Agent
masahirokawahara
1
340
Featured
See All Featured
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
310
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
3k
Technical Leadership for Architectural Decision Making
baasie
3
560
Are puppies a ranking factor?
jonoalderson
2
3.9k
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.2k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
Java REST API Framework Comparison - PWX 2021
mraible
34
9.7k
Test your architecture with Archunit
thirion
2
2.4k
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
260
We Have a Design System, Now What?
morganepeng
55
8.3k
The Illustrated Children's Guide to Kubernetes
chrisshort
51
53k
Transcript
PostgresDoc Using Postgres as a Document Database Myles Braithwaite |
mylesb.ca |
[email protected]
| @mylesb 1
How I Learned to Stop Worrying and Love the SQL
Again Myles Braithwaite | mylesb.ca |
[email protected]
| @mylesb 2
Relational Database Myles Braithwaite | mylesb.ca |
[email protected]
| @mylesb
3
Relational Database Myles Braithwaite | mylesb.ca |
[email protected]
| @mylesb
4
Document Oriented Database { "_id": "b04b9746-77bc-11e6-a75a-34363bd0c9bc", "name": "Lot 9 Pilsner",
"brewer": "Creemore Springs", "rating": 4.2 } Myles Braithwaite | mylesb.ca |
[email protected]
| @mylesb 5
Document Oriented Database { "_id": "b04b9746-77bc-11e6-a75a-34363bd0c9bc", "date": "2016-09-11T16:35:33.773325-0400" "customer": {
"name": "Myles Braithwaite", "telephone": "+1 (416) 555-1234", "address": "123 Street Ave." }, "order": [{ "name": "beer", "price_per_unit": 12, "quantity": 2, "total": 24 }], "total": 24, "payment": { "type": "VISA", "number": "12345", "expiry": "2001-04" } } Myles Braithwaite | mylesb.ca |
[email protected]
| @mylesb 6
Document Oriented Database Myles Braithwaite | mylesb.ca |
[email protected]
|
@mylesb 7
Why not both? Myles Braithwaite | mylesb.ca |
[email protected]
|
@mylesb 8
Why not both? • Avoid complicated JOINs. • Ability to
store complicated JSON API responses in the database. • Avoid transforming data before returning it via a JSON API. Myles Braithwaite | mylesb.ca |
[email protected]
| @mylesb 9
Postgres JSON/JSONB Type Myles Braithwaite | mylesb.ca |
[email protected]
|
@mylesb 10
CREATE TABLE Myles Braithwaite | mylesb.ca |
[email protected]
| @mylesb
11
CREATE TABLE notes ( id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
notebook_id UUID REFERENCES notebooks (id), note JSONB ); Myles Braithwaite | mylesb.ca |
[email protected]
| @mylesb 12
INSERT Myles Braithwaite | mylesb.ca |
[email protected]
| @mylesb 13
INSERT INTO notes VALUES ( '675afef8-7863-11e6-8914-34363bd0c9bc', '8921ad42-300e-447a-8417-ec92bb18e2df', '{ "name": "PostgresDoc",
"tags": ["Postgres", "Document Oriented Database"], "body": "Hello, World" }' ) Myles Braithwaite | mylesb.ca |
[email protected]
| @mylesb 14
SELECT Myles Braithwaite | mylesb.ca |
[email protected]
| @mylesb 15
SELECT note->>'name' AS name FROM notes; name ---------------------------------------- PostgresDoc World
Domination Plans Superman < Batman > Supergirl = Batgirl (3 rows) Myles Braithwaite | mylesb.ca |
[email protected]
| @mylesb 16
SELECT jsonb_array_elements_text(note->'tags') AS tag FROM notes WHERE id = '675afef8-7863-11e6-8914-34363bd0c9bc';
tag --------------------------- Postgres Document Oriented Database (2 rows) Myles Braithwaite | mylesb.ca |
[email protected]
| @mylesb 17
SELECT id, note FROM notes WHERE note->'tags' ? 'Postgres'; id
| note | -------------------------------------+------------------------ 675afef8-7863-11e6-8914-34363bd0c9bc | {"name": "PostgresDoc"} (1 row) Myles Braithwaite | mylesb.ca |
[email protected]
| @mylesb 18
SELECT count(*) FROM notes WHERE note ? 'checklist'; count -------
1 (1 row) Myles Braithwaite | mylesb.ca |
[email protected]
| @mylesb 19
CREATE INDEX idx_checklist ON notes ((note->>'tags')) Myles Braithwaite | mylesb.ca
|
[email protected]
| @mylesb 20
ORMs Myles Braithwaite | mylesb.ca |
[email protected]
| @mylesb 21
SQLAlchemy note_table = Table('notes', metadata, Column('id', Integer, primary_key=True), Column('note', JSONB)
) with engine.connect() as conn: conn.execute( note_table.insert(), note = {"name": "Postgres", "tags": ["Postgres"]} ) Myles Braithwaite | mylesb.ca |
[email protected]
| @mylesb 22
Rails create_table :notes do |t| t.json 'note' end class Note
< ApplicationRecord end Note.create(note: { name: "Postgres", tags: ["Postgres"]}) Myles Braithwaite | mylesb.ca |
[email protected]
| @mylesb 23
https://github.com/myles/2016-09-13-postgresdoc Myles Braithwaite | mylesb.ca |
[email protected]
| @mylesb 24