π POSTGRESQL SEARCH FUNDAMENTALS
Full-text search in PostgreSQL is built-in.
No extensions. No setup. Just SQL.
---
THE BASICS
That's it. That's search.
---
WHAT'S HAPPENING
---
MAKE IT FAST
---
KEEP IT UPDATED
---
RANKING RESULTS
Most relevant first. Like Algolia.
---
Tomorrow: The complete system.
Multi-column search.
Highlighting matches.
Search suggestions.
All copy-paste ready.
3 β
#January2026
Full-text search in PostgreSQL is built-in.
No extensions. No setup. Just SQL.
---
THE BASICS
-- Simple search
SELECT * FROM posts
WHERE to_tsvector('english', title || ' ' || body)
@@ to_tsquery('english', 'postgresql');
That's it. That's search.
---
WHAT'S HAPPENING
to_tsvector = converts text to searchable tokensto_tsquery = converts search term to query@@ = "matches"SELECT to_tsvector('english', 'PostgreSQL is amazing');
-- Result: 'amaz':3 'postgresql':1
-- Words are stemmed: "amazing" β "amaz"
---
MAKE IT FAST
-- Add a search column
ALTER TABLE posts ADD COLUMN search_vector tsvector;
-- Populate it
UPDATE posts SET search_vector =
to_tsvector('english', title || ' ' || COALESCE(body, ''));
-- Index it
CREATE INDEX idx_posts_search ON posts USING GIN(search_vector);
-- Search it (fast now)
SELECT * FROM posts
WHERE search_vector @@ to_tsquery('english', 'postgresql');
---
KEEP IT UPDATED
-- Auto-update on insert/update
CREATE FUNCTION posts_search_update() RETURNS trigger AS $$
BEGIN
NEW.search_vector := to_tsvector('english', NEW.title || ' ' || COALESCE(NEW.body, ''));
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER posts_search_trigger
BEFORE INSERT OR UPDATE ON posts
FOR EACH ROW EXECUTE FUNCTION posts_search_update();
---
RANKING RESULTS
SELECT title,
ts_rank(search_vector, query) as rank
FROM posts, to_tsquery('english', 'postgresql') query
WHERE search_vector @@ query
ORDER BY rank DESC;
Most relevant first. Like Algolia.
---
Tomorrow: The complete system.
Multi-column search.
Highlighting matches.
Search suggestions.
All copy-paste ready.
3 β
#January2026
1Kβ€3π1
This media is not supported in the widget
VIEW IN TELEGRAM
β€2π₯1
PostgreSQL Pro | Database Mastery pinned Β«π COPY-PASTE SEARCH SYSTEM Full-text search that works. No Algolia. No Elasticsearch. No bill. --- WHAT'S INSIDE π Complete Schema - Search vector column - GIN index for speed - Auto-update trigger - Multi-table search π Search Queries - Simple searchβ¦Β»
π― FUZZY MATCHING WITH PG_TRGM
Full-text search is great.
But what about typos?
"postgre" should find "postgresql"
"recieved" should find "received"
Enter: pg_trgm
---
ENABLE IT
That's it. Now you have fuzzy matching.
---
HOW IT WORKS
Trigrams = 3-character chunks
"postgresql" β {" po", "pos", "ost", "stg", "tgr", "gre", "res", "esq", "sql", "ql "}
Similar words share trigrams.
More shared = more similar.
---
SIMILARITY SEARCH
---
MAKE IT FAST
---
ILIKE THAT'S FAST
Remember how LIKE '%search%' is slow?
---
COMBINE WITH FULL-TEXT
Finds exact matches AND fuzzy matches.
---
THAT'S THE WHOLE SEARCH STACK
-
-
- GIN indexes = fast
No external services.
#January2026
Full-text search is great.
But what about typos?
"postgre" should find "postgresql"
"recieved" should find "received"
Enter: pg_trgm
---
ENABLE IT
CREATE EXTENSION pg_trgm;
That's it. Now you have fuzzy matching.
---
HOW IT WORKS
Trigrams = 3-character chunks
"postgresql" β {" po", "pos", "ost", "stg", "tgr", "gre", "res", "esq", "sql", "ql "}
Similar words share trigrams.
More shared = more similar.
---
SIMILARITY SEARCH
-- Find similar to "postgre"
SELECT name, similarity(name, 'postgre') as sim
FROM products
WHERE similarity(name, 'postgre') > 0.3
ORDER BY sim DESC;
---
MAKE IT FAST
-- GIN index for similarity
CREATE INDEX idx_products_name_trgm
ON products USING GIN(name gin_trgm_ops);
-- Now this is fast:
SELECT * FROM products
WHERE name % 'postgre' -- % means "similar to"
ORDER BY name <-> 'postgre'; -- <-> is distance
---
ILIKE THAT'S FAST
Remember how LIKE '%search%' is slow?
-- With pg_trgm, this uses the index:
SELECT * FROM products
WHERE name ILIKE '%data%';
-- Because the GIN index supports it!
---
COMBINE WITH FULL-TEXT
-- Best of both worlds
SELECT title,
ts_rank(search_vector, query) as relevance,
similarity(title, $1) as fuzzy_score
FROM posts, to_tsquery('english', $1) query
WHERE search_vector @@ query
OR title % $1
ORDER BY relevance + fuzzy_score DESC;
Finds exact matches AND fuzzy matches.
---
THAT'S THE WHOLE SEARCH STACK
-
tsvector + tsquery = full-text-
pg_trgm = fuzzy/typo tolerance- GIN indexes = fast
No external services.
#January2026
50β€1
π JANUARY RETROSPECTIVE
First month of 2026: done.
---
WHAT WE COVERED
Week 1: Auth queries
Week 2: Background jobs
Week 3: Performance & indexes
Week 4: Full-text search
Four fundamental patterns.
Zero external dependencies.
Pure PostgreSQL.
---
THE THEME
"You don't need that service."
Auth β No Auth0
Jobs β No Redis
Search β No Algolia
Everything β PostgreSQL
Not because services are bad.
Because simple is good.
---
WHAT WORKED
β Shorter, focused content
β Copy-paste ready code
β Lower prices (3 β)
β One topic per week
---
FEBRUARY PREVIEW
Thinking about:
- Real-time with LISTEN/NOTIFY
- Multi-tenancy patterns
- Backup & recovery
- API rate limiting
What do you want to see?
---
HONEST QUESTION
Is this content useful?
- π₯ Yes, using it in real projects
- π Yes, learning for future
- π Meh, not really
- π No, want something different
Reply or react.
Helps me know what to make.
---
THANK YOU
1,000+ of you here now.
That's not nothing.
See you in February.
@postgres
#January2026
First month of 2026: done.
---
WHAT WE COVERED
Week 1: Auth queries
Week 2: Background jobs
Week 3: Performance & indexes
Week 4: Full-text search
Four fundamental patterns.
Zero external dependencies.
Pure PostgreSQL.
---
THE THEME
"You don't need that service."
Auth β No Auth0
Jobs β No Redis
Search β No Algolia
Everything β PostgreSQL
Not because services are bad.
Because simple is good.
---
WHAT WORKED
β Shorter, focused content
β Copy-paste ready code
β Lower prices (3 β)
β One topic per week
---
FEBRUARY PREVIEW
Thinking about:
- Real-time with LISTEN/NOTIFY
- Multi-tenancy patterns
- Backup & recovery
- API rate limiting
What do you want to see?
---
HONEST QUESTION
Is this content useful?
- π₯ Yes, using it in real projects
- π Yes, learning for future
- π Meh, not really
- π No, want something different
Reply or react.
Helps me know what to make.
---
THANK YOU
1,000+ of you here now.
That's not nothing.
See you in February.
@postgres
#January2026
50π₯1
π‘ FEBRUARY KICKOFF: REAL-TIME WEEK
New month. New topic.
January covered:
- Auth β
- Background jobs β
- Performance β
- Full-text search β
February starts with: Real-time.
---
THE PROBLEM
Your app needs live updates:
- New message notifications
- Activity feeds
- Live dashboards
- Collaborative features
The "normal" solution:
- Pusher: $49/month
- Ably: $29/month
- Firebase: Usage-based $$
- Socket.io + Redis: Complexity
---
THE POSTGRESQL WAY
LISTEN/NOTIFY.
Built into PostgreSQL since forever.
Zero extra services.
Zero extra cost.
---
THIS WEEK
Tue: How LISTEN/NOTIFY works
Wed: π° Copy-paste real-time system (3 β)
Thu: WebSocket patterns
Fri: Week 5 check-in
---
JANUARY RESULTS
4 weeks. 4 topics. 4 utilities.
Small wins > big masterclasses.
Let's keep going.
#February2026
New month. New topic.
January covered:
- Auth β
- Background jobs β
- Performance β
- Full-text search β
February starts with: Real-time.
---
THE PROBLEM
Your app needs live updates:
- New message notifications
- Activity feeds
- Live dashboards
- Collaborative features
The "normal" solution:
- Pusher: $49/month
- Ably: $29/month
- Firebase: Usage-based $$
- Socket.io + Redis: Complexity
---
THE POSTGRESQL WAY
LISTEN/NOTIFY.
Built into PostgreSQL since forever.
Zero extra services.
Zero extra cost.
-- That's it. That's real-time.
NOTIFY my_channel, 'Hello from the database';
---
THIS WEEK
Tue: How LISTEN/NOTIFY works
Wed: π° Copy-paste real-time system (3 β)
Thu: WebSocket patterns
Fri: Week 5 check-in
---
JANUARY RESULTS
4 weeks. 4 topics. 4 utilities.
Small wins > big masterclasses.
Let's keep going.
#February2026
50β€2
π‘ HOW LISTEN/NOTIFY WORKS
PostgreSQL has built-in pub/sub.
Most people don't know this exists.
---
THE BASICS
That's the whole API.
LISTEN. NOTIFY. Done.
---
FROM YOUR APPLICATION
Now every order change broadcasts automatically.
---
IN NODE.JS
---
LIMITATIONS
- Payload max: 8KB (enough for IDs and small data)
- No persistence (miss it if not listening)
- No delivery guarantee
For most apps? Totally fine.
For critical data? Store first, notify second.
---
Tomorrow: The complete system.
Notifications, activity feeds, live dashboards.
All copy-paste ready.
3 β
#February2026
PostgreSQL has built-in pub/sub.
Most people don't know this exists.
---
THE BASICS
-- Connection 1: Subscribe
LISTEN order_updates;
-- Connection 2: Publish
NOTIFY order_updates, '{"order_id": 123, "status": "shipped"}';
-- Connection 1 receives it instantly
That's the whole API.
LISTEN. NOTIFY. Done.
---
FROM YOUR APPLICATION
-- Trigger on table changes
CREATE OR REPLACE FUNCTION notify_order_change()
RETURNS TRIGGER AS $$
BEGIN
PERFORM pg_notify('order_updates', json_build_object(
'action', TG_OP,
'order_id', NEW.id,
'status', NEW.status
)::TEXT);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER order_changed
AFTER INSERT OR UPDATE ON orders
FOR EACH ROW EXECUTE FUNCTION notify_order_change();
Now every order change broadcasts automatically.
---
IN NODE.JS
const { Client } = require('pg');
const client = new Client();
await client.connect();
await client.query('LISTEN order_updates');
client.on('notification', (msg) => {
const data = JSON.parse(msg.payload);
console.log('Order updated:', data);
// Send to WebSocket clients
});
---
LIMITATIONS
- Payload max: 8KB (enough for IDs and small data)
- No persistence (miss it if not listening)
- No delivery guarantee
For most apps? Totally fine.
For critical data? Store first, notify second.
---
Tomorrow: The complete system.
Notifications, activity feeds, live dashboards.
All copy-paste ready.
3 β
#February2026
83
This media is not supported in the widget
VIEW IN TELEGRAM
50
PostgreSQL Pro | Database Mastery pinned Β«π‘ COPY-PASTE REAL-TIME SYSTEM Live updates without Pusher. No monthly bill. No external service. --- WHAT'S INSIDE π Notification System - User notifications table - Real-time delivery via NOTIFY - Unread count tracking - Mark as read π Activity Feedβ¦Β»
π WEBSOCKET PATTERNS
LISTEN/NOTIFY gets data out of PostgreSQL.
WebSockets get it to the browser.
Here's how to connect them.
---
THE SIMPLE PATTERN
PostgreSQL β Node.js listener β WebSocket server β Browser
---
USER-SPECIFIC CHANNELS
---
ROOM-BASED (LIKE CHAT)
---
SCALING NOTE
One Node.js process = one LISTEN connection.
For multiple servers, each listens independently.
PostgreSQL handles the fan-out.
No Redis pub/sub needed.
---
How would you use real-time in your app?
#February2026
LISTEN/NOTIFY gets data out of PostgreSQL.
WebSockets get it to the browser.
Here's how to connect them.
---
THE SIMPLE PATTERN
PostgreSQL β Node.js listener β WebSocket server β Browser
// server.js
const WebSocket = require('ws');
const { Client } = require('pg');
const wss = new WebSocket.Server({ port: 8080 });
const pg = new Client();
// Listen to PostgreSQL
pg.connect();
pg.query('LISTEN updates');
pg.on('notification', (msg) => {
// Broadcast to all connected clients
wss.clients.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
client.send(msg.payload);
}
});
});
---
USER-SPECIFIC CHANNELS
// Track which users are connected
const userConnections = new Map();
wss.on('connection', (ws, req) => {
const userId = authenticateConnection(req);
if (!userConnections.has(userId)) {
userConnections.set(userId, new Set());
// Subscribe to user's channel
pg.query(`LISTEN user_${userId}`);
}
userConnections.get(userId).add(ws);
ws.on('close', () => {
userConnections.get(userId).delete(ws);
});
});
// In notification handler
pg.on('notification', (msg) => {
const userId = msg.channel.replace('user_', '');
const connections = userConnections.get(userId);
connections?.forEach(ws => {
ws.send(msg.payload);
});
});
---
ROOM-BASED (LIKE CHAT)
-- Notify a specific room
SELECT pg_notify(
'room_' || room_id::TEXT,
json_build_object('message', content, 'user', sender)::TEXT
);
// Join room
ws.on('message', (data) => {
const { action, roomId } = JSON.parse(data);
if (action === 'join') {
pg.query(`LISTEN room_${roomId}`);
// Track membership...
}
});
---
SCALING NOTE
One Node.js process = one LISTEN connection.
For multiple servers, each listens independently.
PostgreSQL handles the fan-out.
No Redis pub/sub needed.
---
How would you use real-time in your app?
#February2026
50
π WEEK 5 CHECK-IN
Real-time week: done.
---
THIS WEEK
Mon: February kickoff, LISTEN/NOTIFY intro
Tue: How it works + triggers
Wed: Complete real-time system π°
Thu: WebSocket patterns
---
THE KEY INSIGHT
PostgreSQL already has pub/sub.
LISTEN/NOTIFY has been there since PostgreSQL 9.0.
That was 2010.
Most people still don't know it exists.
Now you do.
---
WHEN TO USE IT
β Good for:
- Notifications (new message, new follower)
- Activity feeds
- Live dashboards
- Presence (who's online)
- Simple chat
β οΈ Consider alternatives for:
- High-frequency updates (100+ per second)
- Messages larger than 8KB
- Guaranteed delivery requirements
---
WHAT DID YOU BUILD?
- π‘ Added real-time to my app
- π Set up notification system
- π Just learning
- π€ Still figuring out use case
---
FEBRUARY PREVIEW
Week 6: Backups & Recovery (don't lose everything)
Week 7: Multi-tenancy patterns
Week 8: TBD - you decide
What should Week 8 be?
---
5 WEEKS DOWN
We've covered:
1. Auth
2. Background jobs
3. Performance
4. Full-text search
5. Real-time
That's a complete PostgreSQL toolkit.
More coming.
#February2026
Real-time week: done.
---
THIS WEEK
Mon: February kickoff, LISTEN/NOTIFY intro
Tue: How it works + triggers
Wed: Complete real-time system π°
Thu: WebSocket patterns
---
THE KEY INSIGHT
PostgreSQL already has pub/sub.
LISTEN/NOTIFY has been there since PostgreSQL 9.0.
That was 2010.
Most people still don't know it exists.
Now you do.
---
WHEN TO USE IT
β Good for:
- Notifications (new message, new follower)
- Activity feeds
- Live dashboards
- Presence (who's online)
- Simple chat
β οΈ Consider alternatives for:
- High-frequency updates (100+ per second)
- Messages larger than 8KB
- Guaranteed delivery requirements
---
WHAT DID YOU BUILD?
- π‘ Added real-time to my app
- π Set up notification system
- π Just learning
- π€ Still figuring out use case
---
FEBRUARY PREVIEW
Week 6: Backups & Recovery (don't lose everything)
Week 7: Multi-tenancy patterns
Week 8: TBD - you decide
What should Week 8 be?
---
5 WEEKS DOWN
We've covered:
1. Auth
2. Background jobs
3. Performance
4. Full-text search
5. Real-time
That's a complete PostgreSQL toolkit.
More coming.
#February2026
50
π₯ Your backups are probably broken. You just don't know it yet.
Here's how most solo devs handle backups:
Step 1: Set up pg_dump in a cron job
Step 2: Forget about it
Step 3: Server dies 8 months later
Step 4: Discover the cron job stopped working 6 months ago
Step 5: Cry
Sound familiar?
The problem isn't making backups. It's making backups that actually work when you need them.
This week we fix that:
π Tuesday β The three backup methods and when to use each
π Wednesday β π° Complete backup & recovery system you can copy-paste (3β)
π Thursday β How to test your backups (the step everyone skips)
π Friday β Check-in
No managed service needed. No S3 bill surprise. Just PostgreSQL doing what PostgreSQL does well.
How do you handle backups right now? Be honest β no judgment π
@postgres
Here's how most solo devs handle backups:
Step 1: Set up pg_dump in a cron job
Step 2: Forget about it
Step 3: Server dies 8 months later
Step 4: Discover the cron job stopped working 6 months ago
Step 5: Cry
Sound familiar?
The problem isn't making backups. It's making backups that actually work when you need them.
This week we fix that:
π Tuesday β The three backup methods and when to use each
π Wednesday β π° Complete backup & recovery system you can copy-paste (3β)
π Thursday β How to test your backups (the step everyone skips)
π Friday β Check-in
No managed service needed. No S3 bill surprise. Just PostgreSQL doing what PostgreSQL does well.
How do you handle backups right now? Be honest β no judgment π
@postgres
501
π οΈ Three backup methods. Pick the right one.
PostgreSQL gives you three options. Each solves a different problem.
1. pg_dump β The snapshot
bash# Full database dump, compressed
pg_dump -Fc -Z 6 mydb > backup_$(date +%Y%m%d).dump
# Restore
pg_restore -d mydb backup_20260210.dump
Good for: Small databases (<10 GB), simple projects, quick exports.
Bad for: Large databases (slow), point-in-time recovery (impossible).
2. pg_basebackup β The full copy
bash# Complete cluster backup
pg_basebackup -D /backups/base \
-Ft -z -P -v \
-U replicator
Good for: Full server restore, setting up replicas.
Bad for: Frequent backups (copies everything every time).
3. WAL archiving β The timeline
sql-- Enable in postgresql.conf
ALTER SYSTEM SET wal_level = replica;
ALTER SYSTEM SET archive_mode = on;
ALTER SYSTEM SET archive_command =
'cp %p /backups/wal/%f';
ALTER SYSTEM SET archive_timeout = 300;
SELECT pg_reload_conf();
Good for: Point-in-time recovery, minimal data loss, large databases.
Bad for: Nothing. This is what you want.
The solo dev answer:
Database < 5 GB?
β pg_dump daily + WAL archiving
Database 5-50 GB?
β pg_basebackup weekly + WAL archiving
Database > 50 GB?
β pg_basebackup weekly + WAL archiving + incremental strategy
WAL archiving is the common thread. It gives you something the others can't: go back to any point in time.
Accidentally deleted production data at 2:47 PM?
Restore base backup β replay WAL β stop at 2:46 PM
Everything back. Like it never happened.
Tomorrow: the complete system that ties all three together.
Copy-paste ready. 3β.
@postgres
PostgreSQL gives you three options. Each solves a different problem.
1. pg_dump β The snapshot
bash# Full database dump, compressed
pg_dump -Fc -Z 6 mydb > backup_$(date +%Y%m%d).dump
# Restore
pg_restore -d mydb backup_20260210.dump
Good for: Small databases (<10 GB), simple projects, quick exports.
Bad for: Large databases (slow), point-in-time recovery (impossible).
2. pg_basebackup β The full copy
bash# Complete cluster backup
pg_basebackup -D /backups/base \
-Ft -z -P -v \
-U replicator
Good for: Full server restore, setting up replicas.
Bad for: Frequent backups (copies everything every time).
3. WAL archiving β The timeline
sql-- Enable in postgresql.conf
ALTER SYSTEM SET wal_level = replica;
ALTER SYSTEM SET archive_mode = on;
ALTER SYSTEM SET archive_command =
'cp %p /backups/wal/%f';
ALTER SYSTEM SET archive_timeout = 300;
SELECT pg_reload_conf();
Good for: Point-in-time recovery, minimal data loss, large databases.
Bad for: Nothing. This is what you want.
The solo dev answer:
Database < 5 GB?
β pg_dump daily + WAL archiving
Database 5-50 GB?
β pg_basebackup weekly + WAL archiving
Database > 50 GB?
β pg_basebackup weekly + WAL archiving + incremental strategy
WAL archiving is the common thread. It gives you something the others can't: go back to any point in time.
Accidentally deleted production data at 2:47 PM?
Restore base backup β replay WAL β stop at 2:46 PM
Everything back. Like it never happened.
Tomorrow: the complete system that ties all three together.
Copy-paste ready. 3β.
@postgres
π1
This media is not supported in the widget
VIEW IN TELEGRAM
PostgreSQL Pro | Database Mastery pinned Β«π Copy-Paste Backup & Recovery System What's inside: π¦ COMPLETE SYSTEM (3 β) 1. AUTOMATED BACKUP SCRIPT - pg_dump for daily snapshots - pg_basebackup for weekly full copies - WAL archiving for continuous protection - Rotation (keep 7 daily, 4β¦Β»
π§ͺ Your backup is worthless until you test the restore.
The number one backup mistake: assuming it works.
Here's a dead simple restore test you can run right now:
#!/bin/bash
# test_restore.sh β Run this weekly
TEST_DB="restore_test_$(date +%s)"
BACKUP_FILE=$(ls -t /backups/dumps/*.dump | head -1)
echo "Testing restore of: $BACKUP_FILE"
# Create temporary database
createdb $TEST_DB
# Restore into it
pg_restore -d $TEST_DB $BACKUP_FILE 2>/dev/null
# Verify: count tables
TABLE_COUNT=$(psql -t -A -d $TEST_DB \
-c "SELECT count(*) FROM information_schema.tables
WHERE table_schema = 'public'")
# Verify: count rows in your main table
ROW_COUNT=$(psql -t -A -d $TEST_DB \
-c "SELECT count(*) FROM users" 2>/dev/null || echo "0")
# Cleanup
dropdb $TEST_DB
# Report
echo "Tables restored: $TABLE_COUNT"
echo "Users found: $ROW_COUNT"
if [ "$TABLE_COUNT" -gt 0 ] && [ "$ROW_COUNT" -gt 0 ]; then
echo "β Backup is valid"
else
echo "β BACKUP FAILED β investigate immediately"
# Add your alert here: curl webhook, send email, etc.
fi
Schedule it:
# Every Sunday at 3 AM
echo "0 3 * * 0 /usr/local/bin/test_restore.sh \
>> /var/log/backup_test.log 2>&1" | crontab -
Three things to check when testing:
1. Can you restore at all?
Sounds obvious. Many dumps silently fail (permissions, disk space, version mismatch).
2. Is the data complete?
Row counts should match production within the backup window.
3. How long does restore take?
If your 50 GB database takes 4 hours to restore, that's your minimum downtime. Plan accordingly.
The script above takes 2 minutes to set up. Runs automatically. Tells you immediately if something breaks.
Two minutes now vs finding out during an emergency at 3 AM.
When was the last time you tested a restore? π
@postgres
The number one backup mistake: assuming it works.
Here's a dead simple restore test you can run right now:
#!/bin/bash
# test_restore.sh β Run this weekly
TEST_DB="restore_test_$(date +%s)"
BACKUP_FILE=$(ls -t /backups/dumps/*.dump | head -1)
echo "Testing restore of: $BACKUP_FILE"
# Create temporary database
createdb $TEST_DB
# Restore into it
pg_restore -d $TEST_DB $BACKUP_FILE 2>/dev/null
# Verify: count tables
TABLE_COUNT=$(psql -t -A -d $TEST_DB \
-c "SELECT count(*) FROM information_schema.tables
WHERE table_schema = 'public'")
# Verify: count rows in your main table
ROW_COUNT=$(psql -t -A -d $TEST_DB \
-c "SELECT count(*) FROM users" 2>/dev/null || echo "0")
# Cleanup
dropdb $TEST_DB
# Report
echo "Tables restored: $TABLE_COUNT"
echo "Users found: $ROW_COUNT"
if [ "$TABLE_COUNT" -gt 0 ] && [ "$ROW_COUNT" -gt 0 ]; then
echo "β Backup is valid"
else
echo "β BACKUP FAILED β investigate immediately"
# Add your alert here: curl webhook, send email, etc.
fi
Schedule it:
# Every Sunday at 3 AM
echo "0 3 * * 0 /usr/local/bin/test_restore.sh \
>> /var/log/backup_test.log 2>&1" | crontab -
Three things to check when testing:
1. Can you restore at all?
Sounds obvious. Many dumps silently fail (permissions, disk space, version mismatch).
2. Is the data complete?
Row counts should match production within the backup window.
3. How long does restore take?
If your 50 GB database takes 4 hours to restore, that's your minimum downtime. Plan accordingly.
The script above takes 2 minutes to set up. Runs automatically. Tells you immediately if something breaks.
Two minutes now vs finding out during an emergency at 3 AM.
When was the last time you tested a restore? π
@postgres
284
π Week 6 done. Your backups should be smarter now.
Quick recap:
β Monday β The honest truth about backup failures
β Tuesday β Three methods: pg_dump, pg_basebackup, WAL archiving
β Wednesday β π° Complete backup system (3β)
β Thursday β Restore testing script
The one thing to take away this week: backups without tested restores are just wasted disk space.
NEXT WEEK: MULTI-TENANCY PATTERNS
Week 7 tackles the question every SaaS builder faces: how do you serve multiple customers from one database?
Shared tables with row-level security
Schema-per-tenant
When to pick which
The copy-paste RLS setup
One of the most requested topics. See you Monday.
What did you implement this week? π
@postgres
Quick recap:
β Monday β The honest truth about backup failures
β Tuesday β Three methods: pg_dump, pg_basebackup, WAL archiving
β Wednesday β π° Complete backup system (3β)
β Thursday β Restore testing script
The one thing to take away this week: backups without tested restores are just wasted disk space.
NEXT WEEK: MULTI-TENANCY PATTERNS
Week 7 tackles the question every SaaS builder faces: how do you serve multiple customers from one database?
Shared tables with row-level security
Schema-per-tenant
When to pick which
The copy-paste RLS setup
One of the most requested topics. See you Monday.
What did you implement this week? π
@postgres
π’ Every SaaS has the same question: how do you serve 100 customers from one database?
Three approaches. Each with tradeoffs.
1οΈβ£ Shared tables + Row-Level Security
One set of tables. Every row has a tenant_id.
PostgreSQL enforces who sees what.
2οΈβ£ Schema-per-tenant
Same database, separate schema for each customer.
tenant_alice.users, tenant_bob.users
3οΈβ£ Database-per-tenant
Fully isolated. One database per customer.
Maximum separation, maximum overhead.
Most solo devs should start with #1.
It's the simplest, cheapest, and scales surprisingly far.
This week:
π Tuesday β Shared tables with RLS (the setup)
π Wednesday β π° Complete multi-tenant system (3β)
π Thursday β When to switch approaches
π Friday β Check-in
If you're building a SaaS, this is your week.
Which approach are you using right now? π
@postgres
Three approaches. Each with tradeoffs.
1οΈβ£ Shared tables + Row-Level Security
One set of tables. Every row has a tenant_id.
PostgreSQL enforces who sees what.
2οΈβ£ Schema-per-tenant
Same database, separate schema for each customer.
tenant_alice.users, tenant_bob.users
3οΈβ£ Database-per-tenant
Fully isolated. One database per customer.
Maximum separation, maximum overhead.
Most solo devs should start with #1.
It's the simplest, cheapest, and scales surprisingly far.
This week:
π Tuesday β Shared tables with RLS (the setup)
π Wednesday β π° Complete multi-tenant system (3β)
π Thursday β When to switch approaches
π Friday β Check-in
If you're building a SaaS, this is your week.
Which approach are you using right now? π
@postgres
β€3
π Row-Level Security in 10 minutes. Your tenants can never see each other's data.
The idea: PostgreSQL checks every query automatically. No WHERE tenant_id = ? scattered through your code.
Step 1: Add tenant_id to every table
ALTER TABLE users ADD COLUMN tenant_id UUID NOT NULL;
ALTER TABLE orders ADD COLUMN tenant_id UUID NOT NULL;
ALTER TABLE products ADD COLUMN tenant_id UUID NOT NULL;
CREATE INDEX idx_users_tenant ON users(tenant_id);
CREATE INDEX idx_orders_tenant ON orders(tenant_id);
CREATE INDEX idx_products_tenant ON products(tenant_id);
Step 2: Create an app user (not the superuser)
CREATE USER app_user WITH PASSWORD 'strong_password';
GRANT USAGE ON SCHEMA public TO app_user;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO app_user;
ALTER DEFAULT PRIVILEGES IN SCHEMA public
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO app_user;
Step 3: Enable RLS
ALTER TABLE users ENABLE ROW LEVEL SECURITY;
ALTER TABLE orders ENABLE ROW LEVEL SECURITY;
ALTER TABLE products ENABLE ROW LEVEL SECURITY;
Step 4: Create policies
CREATE POLICY tenant_isolation ON users
USING (tenant_id = current_setting('app.tenant_id')::UUID);
CREATE POLICY tenant_isolation ON orders
USING (tenant_id = current_setting('app.tenant_id')::UUID);
CREATE POLICY tenant_isolation ON products
USING (tenant_id = current_setting('app.tenant_id')::UUID);
Step 5: Set tenant in your app (every request)
-- At the start of each API request:
SET LOCAL app.tenant_id = 'uuid-of-current-tenant';
-- Now every query is automatically filtered:
SELECT * FROM users;
-- Only returns rows where tenant_id matches. Automatically.
That's it. No middleware. No query wrappers. PostgreSQL handles it at the database level.
Tomorrow: the complete system with signup, onboarding, admin access, cross-tenant queries, and all the edge cases. 3β.
@postgres
The idea: PostgreSQL checks every query automatically. No WHERE tenant_id = ? scattered through your code.
Step 1: Add tenant_id to every table
ALTER TABLE users ADD COLUMN tenant_id UUID NOT NULL;
ALTER TABLE orders ADD COLUMN tenant_id UUID NOT NULL;
ALTER TABLE products ADD COLUMN tenant_id UUID NOT NULL;
CREATE INDEX idx_users_tenant ON users(tenant_id);
CREATE INDEX idx_orders_tenant ON orders(tenant_id);
CREATE INDEX idx_products_tenant ON products(tenant_id);
Step 2: Create an app user (not the superuser)
CREATE USER app_user WITH PASSWORD 'strong_password';
GRANT USAGE ON SCHEMA public TO app_user;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO app_user;
ALTER DEFAULT PRIVILEGES IN SCHEMA public
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO app_user;
Step 3: Enable RLS
ALTER TABLE users ENABLE ROW LEVEL SECURITY;
ALTER TABLE orders ENABLE ROW LEVEL SECURITY;
ALTER TABLE products ENABLE ROW LEVEL SECURITY;
Step 4: Create policies
CREATE POLICY tenant_isolation ON users
USING (tenant_id = current_setting('app.tenant_id')::UUID);
CREATE POLICY tenant_isolation ON orders
USING (tenant_id = current_setting('app.tenant_id')::UUID);
CREATE POLICY tenant_isolation ON products
USING (tenant_id = current_setting('app.tenant_id')::UUID);
Step 5: Set tenant in your app (every request)
-- At the start of each API request:
SET LOCAL app.tenant_id = 'uuid-of-current-tenant';
-- Now every query is automatically filtered:
SELECT * FROM users;
-- Only returns rows where tenant_id matches. Automatically.
That's it. No middleware. No query wrappers. PostgreSQL handles it at the database level.
Tomorrow: the complete system with signup, onboarding, admin access, cross-tenant queries, and all the edge cases. 3β.
@postgres
This media is not supported in the widget
VIEW IN TELEGRAM