PostgreSQL AI Coding Rules

PostgreSQL AI rules help engineering teams get better results from AI coding assistants like Cursor, Windsurf, and GitHub Copilot. By defining clear conventions for code style, architecture patterns, error handling, and module organisation, PostgreSQL AI rules ensure that generated code is consistent, maintainable, and production-ready. Whether you are working on a side project or a large-scale enterprise system, community-curated rules on AI Rules Hub provide a solid foundation you can adopt instantly and customise to fit your team's standards.

Why Use AI Rules for PostgreSQL?

  • Ensure AI-generated PostgreSQL code follows your team's conventions
  • Prevent common anti-patterns that degrade maintainability
  • Reduce code review cycles by getting AI output right the first time
  • Standardise error handling, logging, and module structure
  • Make AI assistants produce secure and performance-conscious code

Best Practices for PostgreSQL AI Coding

Define a Consistent Code Style

Specify formatting preferences (indentation, quotes, trailing commas) for PostgreSQL so AI output matches your linter configuration without manual edits.

Enforce Error Handling Patterns

Instruct AI to always handle errors explicitly, use structured logging, and avoid swallowing exceptions silently.

Set Module Organisation Rules

Define how PostgreSQL modules should be organised — feature folders, barrel exports, and separation of concerns — so AI keeps the project structure clean.

Require Security-Conscious Patterns

Add rules that enforce input validation, sanitisation, and safe dependency usage so AI never introduces obvious security vulnerabilities.

Common Patterns & Standards

#01

Separation of Concerns

Keep business logic, data access, and presentation layers separate in PostgreSQL projects so each layer is independently testable.

#02

Dependency Injection

Pass dependencies explicitly through constructors or function parameters — avoiding global state that makes testing difficult.

#03

Consistent Naming Conventions

Rule AI to follow PostgreSQL community naming standards for files, classes, functions, and constants.

#04

Automated Testing Standards

Define what test types are required (unit, integration) and where test files should live so AI generates tests alongside implementation code.

Top PostgreSQL Rules on AI Rules Hub

Filter: postgresql
Clear all

Production-ready PostgreSQL and Drizzle ORM performance rules for scalable SaaS applications.

# PostgreSQL + Drizzle Performance Rules for Production SaaS

> Production-ready PostgreSQL and Drizzle ORM performance rules for scalable SaaS applications.

---

## Database Query Performance

- Always use indexed columns in `WHERE` clauses.
- Prefer composite indexes when filtering by multiple frequently used columns.
- Avoid `SELECT *` in production queries. Explicitly select only required columns.
- Use `EXPLAIN ANALYZE` before deploying complex queries.
- Never run unbounded queries in API endpoints.

---

## Pagination & Large Data Handling

- Use `LIMIT` and `OFFSET` carefully.
- Prefer keyset pagination for large datasets.
- Avoid loading large datasets into memory.
- Always paginate API responses returning collections.

---

## Indexing Strategy

- Use partial indexes for frequently filtered subsets (e.g., active records).
- Create GIN indexes for full-text search (`tsvector`).
- Monitor index bloat and reindex periodically.
- Avoid over-indexing rarely queried columns.

---

## Drizzle ORM Best Practices

- Avoid N+1 query patterns.
- Prefer server-side filtering over client-side filtering.
- Use transactions for multi-step writes.
- Keep ORM queries predictable and optimized.
- Avoid dynamic raw SQL unless necessary and always sanitize inputs.

---

## Full-Text Search Optimization

- Use `tsvector` with weighted fields.
- Rank results using `ts_rank`.
- Create a GIN index on the search vector.
- Normalize search queries using `plainto_tsquery`.

---

## Data Modeling

- Use appropriate data types (`UUID` instead of `text` for IDs when possible).
- Avoid excessive `JSONB` nesting.
- Keep schema normalized unless denormalization is justified.
- Use foreign key constraints for relational integrity.

---

## Production Safety

- Use connection pooling in production environments.
- Avoid blocking queries inside request lifecycles.
- Implement caching for frequently accessed rule lists.
- Move heavy data processing to background jobs.
- Monitor slow queries using PostgreSQL logs.

---

## Observability & Monitoring

- Enable query logging in staging environments.
- Track performance regressions after migrations.
- Audit long-running transactions.
- Monitor connection pool exhaustion.
29 views

Share Your PostgreSQL AI Rules

Have rules that improved your PostgreSQL workflow? Submit them to AI Rules Hub and help the community get better results from AI coding assistants.

Frequently Asked Questions

PostgreSQL AI rules are context files (like `.cursorrules` or `AGENTS.md`) that instruct AI coding assistants to follow PostgreSQL best practices — covering code style, architecture, error handling, and testing conventions.

Command Palette

Search for a command to run...