Skip to content

omob/mcp-postgres-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MCP PostgreSQL Server

An MCP (Model Context Protocol) server that connects to PostgreSQL databases and enables AI assistants to run read-only queries.

Features

  • query - Execute read-only SQL queries (SELECT only)
  • list_tables - List all tables in the database
  • describe_table - Get column information for any table

Security

This server enforces read-only access through two layers:

1. SQL Validation (Application Layer)

The server rejects destructive operations (DELETE, DROP, INSERT, UPDATE, etc.) before they reach the database.

2. Read-Only Database User (Recommended)

For maximum security, create a dedicated read-only PostgreSQL user:

-- Create read-only user
CREATE USER mcp_readonly WITH PASSWORD 'your_secure_password';

-- Grant connection
GRANT CONNECT ON DATABASE your_database TO mcp_readonly;

\

Installation

npm install
npm run build

Configuration

Set your PostgreSQL connection via environment variables:

# Option 1: Connection string
export DATABASE_URL="postgresql://mcp_readonly:password@host:port/database"

# Option 2: Individual variables
export PGHOST=localhost
export PGPORT=5432
export PGUSER=mcp_readonly
export PGPASSWORD=yourpassword
export PGDATABASE=mydb

Usage with Claude Desktop

Add to your claude_desktop_config.json:

{
  "mcpServers": {
    "postgres": {
      "command": "node",
      "args": ["/path/to/mcp-postgres-server/dist/index.js"],
      "env": {
        "DATABASE_URL": "postgresql://mcp_readonly:password@localhost:5432/mydb"
      }
    }
  }
}

Development

npm run dev

License

MIT

About

MCP server for read-only PostgreSQL database connections and queries

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages