Home / Integrations / PostgreSQL
PostgreSQL
Use Claude Code to design schemas, write complex SQL queries, optimize performance, and manage PostgreSQL migrations with AI assistance.
Setup
- 1.Ensure PostgreSQL client tools are availableClaude Code can run psql commands, pg_dump, and other PostgreSQL tools directly. Make sure the client tools are installed.
psql --version - 2.Set up a connection stringConfigure your database connection so Claude Code can run queries and inspect the schema. Use a .env file or environment variable.
export DATABASE_URL="postgresql://user:password@localhost:5432/mydb" - 3.Start Claude Code with database contextLaunch Claude Code in your project directory. If you have existing migration files or SQL scripts, it will read them to understand your current schema.
claude "describe the current database schema and suggest improvements"
Features
- +Designs normalized database schemas with proper constraints, indexes, and relationships
- +Writes complex SQL queries including CTEs, window functions, and subqueries
- +Analyzes query performance using EXPLAIN ANALYZE and suggests optimizations
- +Generates migration files for schema changes with proper rollback scripts
- +Creates indexes based on actual query patterns and access paths
- +Writes PL/pgSQL functions, triggers, and stored procedures
Use Cases
Describe your application's data model in plain English and Claude Code designs a normalized PostgreSQL schema with tables, constraints, indexes, and relationships. It generates the migration SQL and can run it against your local database.
Paste a slow SQL query and Claude Code will run EXPLAIN ANALYZE, identify bottlenecks (sequential scans, missing indexes, poor join order), and rewrite the query or suggest indexes that improve performance.
Describe the report you need and Claude Code writes the SQL using CTEs, window functions, aggregations, and date calculations. It tests the query against your database and refines it until the output matches your requirements.
Claude Code creates versioned migration files that work with your migration tool (Prisma, Drizzle, Knex, raw SQL). Each migration includes both the up and down scripts for safe rollbacks.
Claude Code as your database expert
PostgreSQL is the most advanced open-source relational database, and its feature set is enormous: JSONB, full-text search, window functions, CTEs, partitioning, custom types, and more. Claude Code understands all of these features and uses them appropriately when designing schemas and writing queries.
What makes Claude Code different from a text-based SQL helper is that it can execute queries. It runs your SQL against the actual database, reads the results, checks EXPLAIN plans, and iterates. This feedback loop produces queries that work correctly and perform well — not just queries that look right.
Example: query optimization workflow
Migration generation
Claude Code generates migration files that follow your project's conventions. Whether you use Prisma's migration system, Drizzle Kit, Knex, or plain SQL files, Claude Code matches the format and naming conventions in your existing migrations directory. Each migration includes rollback logic so you can safely undo changes.
Can Claude Code access my production database?+
Does Claude Code support PostgreSQL extensions?+
Can Claude Code work with ORMs like Prisma or Drizzle?+
How does Claude Code handle sensitive data in queries?+
Related tools
Master Claude Code in days, not months
37 hands-on lessons from beginner to CI/CD automation. Module 1 is free.
START FREE →