An MCP (Model Context Protocol) server that connects to PostgreSQL databases and enables AI assistants to run read-only queries.
query- Execute read-only SQL queries (SELECT only)list_tables- List all tables in the databasedescribe_table- Get column information for any table
This server enforces read-only access through two layers:
The server rejects destructive operations (DELETE, DROP, INSERT, UPDATE, etc.) before they reach the database.
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;
\npm install
npm run buildSet 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=mydbAdd 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"
}
}
}
}npm run devMIT