← Back to MCP servers

Model Context Protocol

PostgreSQL MCP Server

Query and explore PostgreSQL databases with schema inspection and read-only SQL execution.

Connection string

Last reviewed Mar 2, 2026

Install

claude mcp add postgres --transport stdio -- npx -y @modelcontextprotocol/server-postgres postgresql://user:pass@localhost:5432/dbname
Scope: project

Available tools

ToolDescription
queryExecute a read-only SQL query against the database
list_tablesList all tables in the database with their schemas
describe_tableGet column names, types, and constraints for a table

What it does

The PostgreSQL MCP Server connects your AI coding assistant to a PostgreSQL database, enabling it to explore schemas, inspect table structures, and run read-only queries. This gives the assistant deep understanding of your data model when helping you write application code, debug queries, or design new features.

The server operates in read-only mode by default, executing queries within a transaction that gets rolled back. This makes it safe to use against development and staging databases without risk of accidental data modification.

Use cases

  • Explore your database schema to inform AI-assisted code generation for data access layers
  • Debug SQL queries by letting the assistant inspect table structures and run test queries
  • Generate ORM models, migrations, or TypeScript types based on your actual database schema
  • Analyze data patterns by running aggregate queries during development
  • Validate that your application queries match the actual table structures

Getting started

  1. Prepare your PostgreSQL connection string in the format postgresql://user:password@host:port/database. For local development databases, this is typically postgresql://postgres:postgres@localhost:5432/mydb.
  2. Replace the connection string in the install command with your actual database credentials.
  3. The server runs queries in read-only mode by default. All queries are wrapped in a transaction that is rolled back after execution.
  4. For production databases, create a dedicated read-only database user to add an extra layer of safety. Grant SELECT privileges only on the schemas you want the assistant to access.