agentleFS
d-padmanabhand-padmanabhan/agent-engineering-handbook/skills/database-postgresql/SKILL.md

database-postgresql

Skill17 starsChanged 55 days agoMITView on GitHub

PostgreSQL database design patterns, naming conventions, schema design, migrations, performance optimization, and security best practices. Use when designing database schemas, writing migrations, optimizing queries, or working with PostgreSQL-specific features like JSONB, full-text search, or RLS.

  • Deletes or force-pushes

1 other project uses a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

6 rules. About 0 are common, 5 are distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈5

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    "Consistency is king": - Follow naming conventions consistently across all tables and columns

    line 10
  2. Distinctive:

    "Explicit over implicit": - Use explicit constraints, types, and defaults

    line 11
  3. Distinctive:

    "Security by default": - Use parameterized queries, RLS, least privilege

    line 13
  4. Distinctive:

    "Test your migrations": - Test migrations in staging before production

    line 16
  5. Distinctive:

    "Backup before changes": - Always backup before destructive operations

    line 17
  6. Very specific:

    Primary Keys: Always named id (BIGSERIAL)

    line 25

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

PostgreSQL Database Engineering

Core Principles

  • "Consistency is king" - Follow naming conventions consistently across all tables and columns
  • "Explicit over implicit" - Use explicit constraints, types, and defaults
  • "Performance by design" - Index foreign keys and frequently queried columns
  • "Security by default" - Use parameterized queries, RLS, least privilege
  • "Migrations are code" - Version control migrations, make them reversible
  • "Documentation matters" - Comments explain why, not just what
  • "Test your migrations" - Test migrations in staging before production
  • "Backup before changes" - Always backup before destructive operations

Quick Reference

Naming Conventions

  • Tables: Plural snake_case (users, order_items)
  • Columns: Singular snake_case (first_name, email)
  • Primary Keys: Always named id (BIGSERIAL)
  • Foreign Keys: table_name_id pattern (user_id, product_id)
  • Indexes: idx_table_column pattern (idx_users_email)
  • Constraints: Named with descriptive pattern (users_email_unique)

Essential Patterns

Timestamps:

created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL

Soft Deletes:

deleted_at TIMESTAMP WITH TIME ZONE NULL
CREATE INDEX idx_users_deleted_at ON users(deleted_at) WHERE deleted_at IS NULL;

Enums:

CREATE TYPE user_status AS ENUM ('active', 'inactive', 'suspended');
status user_status DEFAULT 'active' NOT NULL

Foreign Keys:

user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE RESTRICT

Common Patterns

Basic Table Structure

CREATE TABLE users (
    id BIGSERIAL PRIMARY KEY,
    email VARCHAR(255) NOT NULL,
    first_name VARCHAR(100) NOT NULL,
    last_name VARCHAR(100) NOT NULL,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,
    
    CONSTRAINT users_email_unique UNIQUE (email)
);

CREATE INDEX idx_users_email ON users(email);

Migration Pattern

-- migrations/20250115120000_create_users_table.sql
CREATE TABLE users (
    id BIGSERIAL PRIMARY KEY,
    email VARCHAR(255) NOT NULL UNIQUE,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL
);

-- Always provide down migration
-- migrations/20250115120000_create_users_table.down.sql
DROP TABLE IF EXISTS users;

Security Essentials

Always use parameterized queries:

# GOOD
cursor.execute("SELECT * FROM users WHERE email = %s", (email,))

# BAD - SQL injection risk
cursor.execute(f"SELECT * FROM users WHERE email = '{email}'")

Row-Level Security for multi-tenant:

ALTER TABLE orders ENABLE ROW LEVEL SECURITY;
CREATE POLICY orders_user_policy ON orders
    FOR ALL USING (user_id = current_setting('app.user_id')::bigint);

Performance Basics

Index foreign keys:

CREATE INDEX idx_orders_user_id ON orders(user_id);

Partial indexes for filtered queries:

CREATE INDEX idx_users_active ON users(email) WHERE deleted_at IS NULL;

Composite indexes for common patterns:

CREATE INDEX idx_orders_user_created ON orders(user_id, created_at DESC);

When to Use This Skill

  • Designing database schemas
  • Writing SQL migrations
  • Optimizing database queries
  • Implementing database security (RLS, parameterized queries)
  • Working with PostgreSQL-specific features (JSONB, full-text search, arrays)
  • Performance tuning (indexes, query plans, partitioning)
  • Database testing and migrations

References

For detailed guidance, see:

  • references/schema-design.md - Naming conventions, table design patterns
  • references/migrations.md - Migration best practices, reversible migrations
  • references/performance.md - Indexing strategies, query optimization
  • references/security.md - RLS, parameterized queries, connection pooling
  • references/sql-safety.md - SQL categories (DQL/DML/DDL/DCL/TCL) and destructive-operation guardrails
  • references/advanced-patterns.md - JSONB, full-text search, CTEs, partitioning

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

Reviews can't be read right now.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.