PO

postgresql

Advanced SQL helper for PostgreSQL schema design and high-performance query optimization.

Install

mkdir -p .claude/skills/postgresql-ssrjkk && curl -L -o skill.zip "https://agentskills.codes/api/skills/download/19122" && unzip -o skill.zip -d .claude/skills/postgresql-ssrjkk && rm skill.zip

Installs to .claude/skills/postgresql-ssrjkk

Activation

This is the description your AI agent reads to decide when to run this skill — the better it matches your request, the more reliably it fires.

Models relational data, writes optimized queries, and manages PostgreSQL databases with indexes, views, and CTEs. Use for robust data storage.
142 chars✓ has a “when” trigger
Beginner

Key capabilities

  • →Create tables with `SERIAL PRIMARY KEY` and `NOT NULL` constraints
  • →Add unique constraints to columns like email
  • →Create indexes on frequently queried columns
  • →Write SQL queries with `LEFT JOIN` and `GROUP BY` clauses
  • →Filter grouped results using `HAVING` clauses
  • →Order query results by aggregate functions

How it works

The skill provides SQL examples for creating tables, indexes, and performing complex queries to manage relational data within a PostgreSQL database.

Inputs & outputs

You give it
SQL DDL and DML statements
You get back
a PostgreSQL database schema and query results

When to use postgresql

  • →Modeling relational data
  • →Writing complex SQL joins
  • →Creating database indexes
  • →Performance tuning queries

About this skill

PostgreSQL

Designing and operating reliable PostgreSQL databases.

Quick Start

docker run --name pg -e POSTGRES_PASSWORD=secret -p 5432:5432 -d postgres:17
psql -h localhost -U postgres
# CREATE DATABASE app;

When to Use

  • Relational data with strong consistency needs
  • Transactions and complex joins
  • Full-text search, JSONB, and geospatial (PostGIS)
  • Analytics via materialized views and window functions

Best Practices

Schema Design

  • Use proper types (uuid, timestamptz, jsonb) — avoid text-only columns
  • Add foreign keys and constraints to enforce integrity
  • Prefer normalized core; denormalize only for known hot reads
  • Name tables in plural or singular consistently; use snake_case

Indexing

  • Index columns used in WHERE, JOIN, ORDER BY
  • Prefer B-tree for equality/range; GIN for JSONB/arrays; BRIN for huge tables
  • Create composite indexes matching query column order
  • Remove unused indexes; analyze with pg_stat_user_indexes

Performance

  • Use EXPLAIN (ANALYZE, BUFFERS) to read plans
  • Avoid SELECT *; fetch only needed columns
  • Batch inserts with COPY or multi-row VALUES
  • Use connection pooling (PgBouncer) in production

Dependencies

docker run --name pg -e POSTGRES_PASSWORD=secret -p 5432:5432 -d postgres:17
# psql client:  psql -h localhost -U postgres

Examples

-- Schema with types, constraints, and indexes
CREATE TABLE users (
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  email text NOT NULL UNIQUE,
  role text NOT NULL DEFAULT 'member',
  created_at timestamptz NOT NULL DEFAULT now()
);

CREATE INDEX idx_users_created ON users (created_at DESC);
-- Query optimization with EXPLAIN
EXPLAIN (ANALYZE, BUFFERS)
SELECT id, email FROM users
WHERE created_at > now() - interval '7 days'
ORDER BY created_at DESC
LIMIT 50;
-- Window function for analytics
SELECT
  category,
  revenue,
  RANK() OVER (PARTITION BY category ORDER BY revenue DESC) AS rnk
FROM monthly_sales;
-- JSONB querying with GIN index
CREATE INDEX idx_meta ON orders USING gin (meta);
SELECT id FROM orders WHERE meta @> '{"status": "paid"}';

Step-by-Step

  1. Model entities and relationships; decide types for each column.
  2. Write the DDL with constraints (PK, FK, UNIQUE, NOT NULL).
  3. Add indexes for the query patterns you actually use.
  4. Write migrations (Alembic/Prisma/Flyway) and version them.
  5. Load representative data and run EXPLAIN (ANALYZE) on hot queries.
  6. Tune indexes and queries; add pooling for production.
  7. Set up backups (pg_dump/WAL archiving) and a restore drill.
  8. Monitor slow queries via pg_stat_statements and set alerts.

Validation

  1. Schema applies cleanly via migrations
  2. Constraints reject invalid data
  3. Hot queries run within target latency
  4. EXPLAIN shows index usage on large tables
  5. Backup and restore verified in staging

Troubleshooting

  • Slow query despite index: check composite index column order and = NULL.
  • Lock waits: long transactions hold locks — keep them short.
  • High memory: raise shared_buffers and effective_cache_size carefully.

When not to use it

  • →When unstructured data is being stored
  • →When simple key-value storage is sufficient
  • →When complex transactional workloads are not a requirement

Prerequisites

psql client

Limitations

  • →Requires PostgreSQL to be installed
  • →Focuses on relational data structures
  • →Does not cover NoSQL database patterns

How it compares

This workflow focuses on specific PostgreSQL features like `SERIAL PRIMARY KEY`, `CREATE INDEX`, and advanced SQL constructs, which are tailored for reliable relational data management compared to generic database operations.

Compared to similar skills

postgresql side by side with the closest alternatives in the catalog.

SkillInstallsUpdatedSafetyDifficulty
postgresql (this skill)03moReviewBeginner
sql-optimization-patterns645moNo flagsAdvanced
drizzle-orm324moNo flagsIntermediate
postgres-patterns207moNo flagsIntermediate

Try saying

Example prompts that trigger this skill in your AI assistant.

Search skills

Search the agent skills registry