PostgreSQL Connector (data-connectors-ai)
Read-only MCP server for PostgreSQL: list schemas/tables, run guarded SELECT-only queries. Session-level read-only enforcement, not string-matching.
Documentation
PostgreSQL MCP Server
Read-only MCP server exposing PostgresClient to AI agents: list schemas/tables, run guarded SQL queries.
Tools
| Tool | Description |
|---|---|
list_schemas() -> list[str] | List all schemas in the database |
list_tables(schema: str = "public") -> list[str] | List tables in a schema |
query(sql: str) -> list[tuple] | Run a query, enforced read-only at the Postgres session level |
table_exists(table_name: str, schema: str = "public") -> bool | Check whether a table exists |
query does not classify SQL text — it runs it on a connection pinned to READ ONLY (conn.set_session(readonly=True, ...)), so Postgres itself rejects any write. See server.py for why a regex/keyword blocklist was rejected in favor of this.
Environment Variables
| Variable | Required | Default | Notes |
|---|---|---|---|
POSTGRES_HOST | yes | — | |
POSTGRES_PORT | no | 5432 | |
POSTGRES_DATABASE | yes | — | |
POSTGRES_USER | yes | — | Used by list_schemas/list_tables and as the fallback for query |
POSTGRES_PASSWORD | yes | — | |
POSTGRES_READONLY_USER | no | falls back to POSTGRES_USER | Used by query if set — see below |
POSTGRES_READONLY_PASSWORD | no | falls back to POSTGRES_PASSWORD |
Recommended: a dedicated read-only DB role
The session-level READ ONLY flag is one line of defense. For a second, DB-enforced line, run setup_readonly_role.sql once against your database (swap the placeholder database name and password first), then set POSTGRES_READONLY_USER/POSTGRES_READONLY_PASSWORD to that role's credentials. query will use it; list_schemas/list_tables keep using POSTGRES_USER.
Run locally
pip install "data-connectors-ai[postgres,mcp]"
data-connectors-postgres-mcp
Use with an MCP client
{
"mcpServers": {
"postgres": {
"command": "uvx",
"args": ["--from", "data-connectors-ai[postgres,mcp]", "data-connectors-postgres-mcp"],
"env": {
"POSTGRES_HOST": "localhost",
"POSTGRES_DATABASE": "your_database",
"POSTGRES_USER": "your_user",
"POSTGRES_PASSWORD": "your_password"
}
}
}
}
Registry
Published at io.github.nagarjunr/data-connectors-postgres on the official MCP registry.