postgres-mcp-server
Provides tools for interacting with PostgreSQL databases, enabling schema discovery, table inspection, database, role, and permission listing, and running SQL queries (read-only by default, with optional write support).
Click on "Install Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@postgres-mcp-serverwhat tables are in the public schema?"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
Postgres MCP Server
A Model Context Protocol (MCP) server for PostgreSQL databases. Connects AI assistants (Claude Desktop, Cursor, Antigravity, etc.) to a PostgreSQL database with schema discovery, catalog introspection, read-only analytical queries, and opt-in safe write operations.
Features
Schema Discovery: Inspect schemas, tables, views, column data types, primary keys, and uniqueness constraints (
list_tables,describe_table).Catalog & Governance Discovery: Discover visible databases, roles with attributes and memberships, and permissions across schemas, tables, and columns (
list_databases,list_roles,list_permissions).Bounded Read Queries: Run parameterized SQL queries with pagination (
limit,offset), strict maximum row limits, and automatic truncation detection (run_query).Gated Safe Writes: Write operations (
run_write_queryfor INSERT, UPDATE, DELETE, DDL) are disabled by default and require explicitALLOW_WRITE=1configuration.Security & Privacy First: Zero credential leakage. Connection strings, passwords, and internal stack traces are redacted from logs and tool responses. Parameterized SQL prevents SQL injection.
Stdio Transport: Seamlessly runs over stdio conforming to standard MCP protocol clients.
Related MCP server: PostgreSQL MCP Server
Quick Start
Running via NPX
Pass the connection string directly as a command-line argument or via environment variable:
# Read-only mode (default)
npx @allensandiego/postgres-mcp-server postgres://user:password@localhost:5432/mydb
# Enable write mode via CLI flag
npx @allensandiego/postgres-mcp-server postgres://user:password@localhost:5432/mydb --allow-write
# Or configure via environment variables
export DATABASE_URL="postgres://user:password@localhost:5432/mydb"
export ALLOW_WRITE=1 # Optional: enable write queries
npx @allensandiego/postgres-mcp-serverGlobal Installation
npm install -g @allensandiego/postgres-mcp-server
# Run read-only
postgres-mcp-server postgres://user:password@localhost:5432/mydb
# Run with write operations enabled
postgres-mcp-server postgres://user:password@localhost:5432/mydb --allow-writeLocal Development
# Clone and install dependencies
git clone https://github.com/allensandiego/postgres-mcp-server.git
cd postgres-mcp-server
npm install
# Build
npm run build
# Run with tsx in development
npm run dev -- postgres://user:password@localhost:5432/mydb --allow-writeConfiguration
The server can be configured via CLI flags or environment variables:
Connection String
You can provide the connection string in any of the following ways (in order of precedence):
CLI Positional Argument:
postgres-mcp-server postgres://user:password@host:port/dbCLI Option:
postgres-mcp-server --url=postgres://...or--connection-string=...Environment Variables:
DATABASE_URL,POSTGRES_URL,POSTGRES_CONNECTION_STRING,PG_CONNECTION_STRING,DATABASE_URI,POSTGRES_URI,PGURL, orPG_URL
Enabling Write Operations (ALLOW_WRITE)
By default, the server runs in read-only mode (run_write_query will reject any destructive or mutating SQL).
To enable write queries (INSERT, UPDATE, DELETE, CREATE, DROP, ALTER):
Via CLI flag: Pass
--allow-write,--write, or-wVia Environment Variable: Set
ALLOW_WRITE=1(orALLOW_WRITE=true)
Environment Variables Reference
Variable | Description | Default |
| Full PostgreSQL connection URI ( | None |
| Enables write queries ( |
|
| Database host name |
|
| Database port number |
|
| Database name |
|
| Database user name |
|
| Database password | None |
| SSL configuration mode ( | Disabled |
| Maximum rows returned per query |
|
| Per-query timeout in milliseconds |
|
| Maximum active database connections in pool |
|
MCP Tools Reference
1. list_tables
Discover all user schemas and their tables/views and columns without writing SQL.
Arguments:
schema(optional string): Filter tables by schema name (e.g."public").
Output: Array of
{ schema, name, type, columns: [{ name, dataType, nullable, isPrimaryKey, isUnique }] }.
2. describe_table
Retrieve detailed column specifications and primary key definitions for a table.
Arguments:
schema(required string): Schema name (e.g."public").table(required string): Table name (e.g."users").
Output:
{ schema, table, columns: [...], primaryKey?: string }.
3. list_databases
Discover databases visible and connectable to the connected user.
Arguments: None.
Output: Array of
{ name, owner, encoding, isTemplate, connectable }.
4. list_roles
Discover roles/users, their administrative attributes, and group memberships.
Arguments: None.
Output: Array of
{ name, superuser, canLogin, canCreateDb, canCreateRole, canBypassRls, memberOf, members }.
5. list_permissions
Discover granted privileges across schemas, tables, and columns.
Arguments:
objectType(optional string):"schema","table", or"column".schema(optional string): Schema name filter.table(optional string): Table name filter.
Output: Array of
{ grantor, grantee, objectType, objectName, privilege, grantable }.
6. run_query
Execute a read-only parameterized SELECT query.
Arguments:
sql(required string): Parameterized SQL statement (e.g."SELECT * FROM orders WHERE status = $1").params(optional array): Parameter substitution values.limit(optional integer): Page limit (capped atMAX_ROW_LIMIT).offset(optional integer): Page offset for pagination.role(optional string): Role/user to assume (SET ROLE) for this specific query only.
Output:
{ columns, rows, rowCount, truncated }.
7. run_write_query
Execute modifying SQL statements (INSERT, UPDATE, DELETE, DDL). Only active when ALLOW_WRITE=1 or --allow-write is provided.
Arguments:
sql(required string): SQL write statement.params(optional array): Parameter values.role(optional string): Role/user to assume (SET ROLE) for this specific write query only.
Output:
{ rowCount }.
8. set_role
Set the active PostgreSQL role/user for the session (SET ROLE) or restore the default session user (RESET ROLE).
Arguments:
role(required string): Role/username to set (e.g."analyst","app_readonly", or"NONE"/"RESET"to return to the original session user).
Output:
{ activeRole, sessionUser, isReset, message }.
MCP Client Setup Examples
Gemini CLI Configuration (mcp_config.json or settings.json)
Read-only mode (Default):
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": [
"-y",
"@allensandiego/postgres-mcp-server@latest",
"postgres://username:password@localhost:5432/mydb"
]
}
}
}Write-enabled mode:
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": [
"-y",
"@allensandiego/postgres-mcp-server@latest",
"postgres://username:password@localhost:5432/mydb",
"--allow-write"
]
}
}
}Or via environment variables:
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": ["-y", "@allensandiego/postgres-mcp-server@latest"],
"env": {
"DATABASE_URL": "postgres://username:password@localhost:5432/mydb",
"ALLOW_WRITE": "1"
}
}
}
}Claude Desktop Configuration (claude_desktop_config.json)
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": [
"-y",
"@allensandiego/postgres-mcp-server@latest",
"postgres://username:password@localhost:5432/mydb"
],
"env": {
"ALLOW_WRITE": "0"
}
}
}
}Antigravity / Cursor Configuration
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": [
"-y",
"@allensandiego/postgres-mcp-server@latest",
"postgres://username:password@localhost:5432/mydb"
],
"env": {
"ALLOW_WRITE": "1"
}
}
}
}Testing & Quality Gates
Run the automated test suite (unit + contract + integration tests):
npm testType checking:
npm run typecheckLinting:
npm run lintLicense
This project is licensed under the PolyForm Noncommercial License 1.0.0 - free for personal, educational, research, and non-commercial open-source use. Commercial use requires a commercial license.
This server cannot be installed
Maintenance
Resources
Unclaimed servers have limited discoverability.
Looking for Admin?
If you are the server author, to access and configure the admin panel.
Related MCP Connectors
- SupabaseOAuthcom.supabase
MCP server for interacting with the Supabase platform
- mcpOAuthcom.gibsonai
GibsonAI MCP server: manage your databases with natural language
Hosted MCP server for PostgreSQL diagnostics: slow queries, missing indexes, connection pressure.
Related MCP Servers
- FlicenseNot gradedqualityDmaintenanceAn MCP server that enables AI assistants to interact with PostgreSQL databases by executing SQL queries and inspecting database schemas. It provides tools for standardized database exploration and management through the Model Context Protocol.
- AlicenseNot gradedqualityDmaintenanceAn open-source MCP server for PostgreSQL schema introspection and guarded read-only queries. It enables MCP clients to discover schemas, tables, columns, indexes, relationships, and safe queryable data from a configured PostgreSQL database.13MIT
- AlicenseNot gradedqualityDmaintenanceMCP server for PostgreSQL that enables Cursor and other MCP clients to execute SQL queries, list tables, and explore database schemas via HTTP.2,422MIT
- AlicenseNot gradedqualityDmaintenanceA read-only MCP server for PostgreSQL that enables safe database introspection and querying via natural language.751MIT
Latest Blog Posts
- Who's Calling? MCP Hosts Are an Identity Blind Spot (And the Spec Knows It)By Om-Shree-0709 on .mcpAgent IdentityOAuth 2.1
- Your AI Chatbot Just Exposed Your CEO's Salary to an InternBy Om-Shree-0709 on .Agent IdentityMCP SecurityOAuth Delegation
- Why MCP Servers Need Execution Sandboxing (And Why Your Current Stack Isn't Enough)By Om-Shree-0709 on .Agentic AiPrompt InjectionWebAssembly
MCP directory API
We provide all the information about MCP servers via our MCP API.
curl -X GET 'https://glama.ai/api/mcp/v1/servers/allensandiego/postgres-mcp-server'
If you have feedback or need assistance with the MCP directory API, please join our Discord server