Skills postgresql
git clone https://github.com/TerminalSkills/skills
T=$(mktemp -d) && git clone --depth=1 https://github.com/TerminalSkills/skills "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/postgresql" ~/.claude/skills/terminalskills-skills-postgresql && rm -rf "$T"
skills/postgresql/SKILL.mdPostgreSQL
Overview
PostgreSQL is an advanced relational database with features that often eliminate the need for separate tools: JSONB for semi-structured data, built-in full-text search, window functions for analytics, recursive CTEs for hierarchical queries, row-level security for multi-tenant isolation, and streaming replication for high availability. It supports partitioning, multiple index types (B-tree, GIN, GiST, BRIN), and connection pooling via PgBouncer.
Instructions
- When designing schemas, use
primary keys withUUID
,gen_random_uuid()
for all timestamps, appropriate constraints (CHECK, UNIQUE, foreign keys with ON DELETE), and partitioning for time-series data.TIMESTAMP WITH TIME ZONE - When working with JSON, use
for truly dynamic data with GIN indexes for containment queries, but prefer proper columns for known fields since they provide better validation and performance.JSONB - When optimizing queries, add indexes based on
output rather than guesswork, use partial indexes for filtered queries, expression indexes for computed values, and covering indexes withEXPLAIN ANALYZE
for index-only scans.INCLUDE - When building full-text search, create
generated columns with GIN indexes, usetsvector
for relevance scoring, and choose the appropriate language configuration for stemming.ts_rank() - When implementing multi-tenancy, use row-level security (RLS) policies for database-level isolation rather than application-level checks, setting the user context via
.current_setting() - When managing production databases, use PgBouncer for connection pooling, monitor with
, runpg_stat_statements
after bulk operations, and set up streaming replication with Patroni for high availability.VACUUM ANALYZE
Examples
Example 1: Design a multi-tenant SaaS database with RLS
User request: "Set up a PostgreSQL database with row-level security for multi-tenant isolation"
Actions:
- Create tables with a
column andtenant_id
primary keysUUID - Enable RLS with
ALTER TABLE ... ENABLE ROW LEVEL SECURITY - Create policies using
for per-request isolationcurrent_setting('app.tenant_id') - Set up connection pooling with PgBouncer and configure
per connectionapp.tenant_id
Output: A multi-tenant database where tenant data is isolated at the database level, preventing cross-tenant data leaks.
Example 2: Add full-text search to a content platform
User request: "Implement search across articles with relevance ranking and highlighting"
Actions:
- Add a
generated column usingsearch_vectorto_tsvector('english', title || ' ' || body) - Create a GIN index on the search vector column
- Build a search query using
with@@
and rank results withplainto_tsquery()ts_rank() - Add
for highlighting matched terms in resultsts_headline()
Output: A fast full-text search with relevance ranking, highlighting, and GIN index-backed performance.
Guidelines
- Use
primary keys to avoid sequential ID enumeration and merge conflicts.UUID - Use
for all timestamps; never useTIMESTAMP WITH TIME ZONE
which loses timezone context.TIMESTAMP - Add indexes based on
output, not guesswork; measure before optimizing.EXPLAIN ANALYZE - Use connection pooling (PgBouncer) for applications with more than 20 connections since PostgreSQL forks a process per connection.
- Use RLS for multi-tenant applications since database-level isolation is more reliable than application-level checks.
- Use
for truly dynamic data, not as a replacement for proper columns.JSONB - Run
after bulk operations since stale statistics lead to bad query plans.VACUUM ANALYZE