Skip to content
postgres-best-practices logo

PostgreSQL Best Practices

postgres-best-practices

PostgreSQL and Supabase optimization guide.

SKILL.md

Full skill instructions

PostgreSQL Best Practices

Goal: Design scalable, secure, and performant database schemas and queries, specifically optimized for Supabase/​PostgreSQL environments.

1. Schema Design

  • Normalization: Aim for 3NF (Third Normal Form) to reduce redundancy.
  • Primary Keys: Use UUID (v7 or v4) or BIGINT (Identity) for primary keys. Avoid simplistic auto-increment INT if scaling horizontally.
  • Naming: Use snake_case for table and column names. Plural for tables (users, orders).
  • Foreign Keys: Always define foreign key constraints to ensure data integrity. Index all FK columns.

2. Performance & Indexing

  • Indexes:
    • B-Tree: Default for equality and range queries.
    • GIN: For JSONB and Full-Text Search (tsvector).
    • GiST: For Geo-spatial data (PostGIS).
  • Composite Indexes: Create composite indexes for columns frequently queried together (order matters: equality first, then range).
  • Explain Analyze: Always check EXPLAIN ANALYZE on complex queries to verify index usage.

3. Security (Row Level Security - RLS)

  • Enable RLS: ALTER TABLE "table_name" ENABLE ROW LEVEL SECURITY;
  • Policies: Define strict policies for SELECT, INSERT, UPDATE, DELETE.
    • Example: auth.uid() = user_id for user-private data.
  • Service Role: Use service role keys only for backend admin tasks; never expose to client.

4. JSONB vs Relational

  • When to use JSONB:
    • Dynamic payload (e.g., webhook responses, configuration settings).
    • Data structure varies significantly per row.
  • When NOT to use JSONB:
    • Data that needs frequent joining or aggregating.
    • Data that requires foreign key constraints.

5. Application Usage (Prisma/​Supabase JS)

  • Connection Pooling: Use Supabase Transaction Pooler (port 6543) for serverless functions to avoid connection exhaustion.
  • N+1 Problem: Be wary of N+1 queries in ORMs (Prisma). Use .include() or explicit joins carefully.

Supabase Specific:

  • Realtime: Only enable Realtime for tables that specifically need it (to save resources).
  • Storage: Use RLS policies for Storage Buckets file access as well.

More skills from Dokhacgiakhoa

langchain-architecture logo
Dokhacgiakhoa/antigravity-ide

langchain-architecture

Design LLM applications using the LangChain framework with agents, memory, and tool integration patterns. Use when building LangChain applications, implementing AI agents, or creating complex LLM workflows.

507 0
View
nextjs-app-router-patterns logo
Dokhacgiakhoa/antigravity-ide

nextjs-app-router-patterns

Master Next.js 14+ App Router with Server Components, streaming, parallel routes, and advanced data fetching. Use when building Next.js applications, implementing SSR/SSG, or optimizing React Server Components.

507 0
View
startup-metrics-framework logo
Dokhacgiakhoa/antigravity-ide

startup-metrics-framework

This skill should be used when the user asks about "key startup

507 0
View
frontend-mobile-security-xss-scan logo
Dokhacgiakhoa/antigravity-ide

frontend-mobile-security-xss-scan

You are a frontend security specialist focusing on Cross-Site Scripting (XSS) vulnerability detection and prevention. Analyze React, Vue, Angular, and vanilla JavaScript code to identify injection poi

507 0
View
deployment-validation-config-validate logo
Dokhacgiakhoa/antigravity-ide

deployment-validation-config-validate

You are a configuration management expert specializing in validating, testing, and ensuring the correctness of application configurations. Create comprehensive validation schemas, implement configurat

507 0
View

This skill should be used when the user asks to "test for XSS vulnerabilities", "perform cross-site scripting attacks", "identify HTML injection flaws", "exploit client-side injection vulnerabilities", "steal cookies via XSS", or "bypass content security policies". It provides comprehensive techn...

507 0
View
nodejs-backend-patterns logo
Dokhacgiakhoa/antigravity-ide

nodejs-backend-patterns

Build production-ready Node.js backend services with Express/Fastify, implementing middleware patterns, error handling, authentication, database integration, and API design best practices. Use when creating Node.js servers, REST APIs, GraphQL backends, or microservices architectures.

507 0
View
projection-patterns logo
Dokhacgiakhoa/antigravity-ide

projection-patterns

Build read models and projections from event streams. Use when implementing CQRS read sides, building materialized views, or optimizing query performance in event-sourced systems.

507 0
View
backend-architect logo
Dokhacgiakhoa/antigravity-ide

backend-architect

Expert backend architect specializing in scalable API design,

507 0
View
seo-content-planner logo
Dokhacgiakhoa/antigravity-ide

seo-content-planner

Creates comprehensive content outlines and topic clusters for SEO.

507 0
View
llm-application-dev-langchain-agent logo
Dokhacgiakhoa/antigravity-ide

llm-application-dev-langchain-agent

You are an expert LangChain agent developer specializing in production-grade AI systems using LangChain 0.1+ and LangGraph.

507 0
View

Popular AI tools

Kaiber logo
Video

Kaiber

Generate, edit, and beat-sync AI video with leading models in one workspace.

Paid
View
Vimcal logo
Productivity

Vimcal

The world's fastest calendar for remote work

Free
View

Transform Your Design with AI Designer by ImgCreator.ai

Freemium
View
Akool AI logo
Content & writing

Akool AI

Revolutionizing Video Production with AI-Powered Creativity

Paid
View

Extend an image past the frame and let AI fill the new aspect ratio.

Freemium
View
StarByFace logo
Security

StarByFace

Discover your celebrity doppelgänger with StarByFace!

Free
View
C

ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.

Freemium
View
Opus Clip logo
Coding & apps

Opus Clip

Opus.ai: Revolutionize Your Web Experience

Free
View