Skip to content

Home / Integrations / PostgreSQL

Database

PostgreSQL

Use Claude Code to design schemas, write complex SQL queries, optimize performance, and manage PostgreSQL migrations with AI assistance.

Setup

  1. 1.
    Ensure PostgreSQL client tools are available
    Claude Code can run psql commands, pg_dump, and other PostgreSQL tools directly. Make sure the client tools are installed.
    psql --version
  2. 2.
    Set up a connection string
    Configure 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. 3.
    Start Claude Code with database context
    Launch 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

Schema design from requirements

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.

Query optimization

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.

Complex reporting queries

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.

Database migration management

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?+
Claude Code can connect to any database your terminal has access to. For safety, point it at a local or staging database during development. Only connect to production for read-only investigations like query optimization analysis.
Does Claude Code support PostgreSQL extensions?+
Yes. Claude Code understands popular extensions like pg_trgm (fuzzy search), uuid-ossp, pgcrypto, PostGIS (geospatial), and pg_stat_statements (query performance). It will suggest installing relevant extensions when they would benefit your use case.
Can Claude Code work with ORMs like Prisma or Drizzle?+
Absolutely. Claude Code reads your Prisma schema or Drizzle definitions and generates migrations that match. It can also write raw SQL when the ORM's query builder is insufficient for complex queries.
How does Claude Code handle sensitive data in queries?+
Claude Code sends query text and results to the Anthropic API as part of its context. Avoid running it against databases with sensitive PII unless your organization's data policies permit this. Use anonymized staging databases when possible.

Related tools

Claude CodeAider

Related terms

Programare AgenticăProtocolul de Context al Modelului (MCP)Agent de Programare

Master Claude Code in days, not months

37 hands-on lessons from beginner to CI/CD automation. Module 1 is free.

START FREE →
← ALL INTEGRATIONS