Omniscio

OmniscioGuidesPostgres MCP server

Giving Claude Code read-only access to Postgres

A Postgres server lets Claude answer questions about your own data: which rows broke a constraint, what the slow query actually returned, whether last week's migration did what you think.

It is also the server where the wrong choice costs the most, because a database is not a read-only API. Two decisions matter more than the command: which server you install, and whether it can write.

Sources read November 25, 2026. By the Omniscio team. Updated .

Which Postgres server

The PostgreSQL server that older tutorials name, @modelcontextprotocol/server-postgres, is no longer in the active reference list. The repository that used to hold it lists it under archived servers, described as read-only database access with schema inspection.

That does not make it a trap you must avoid at all costs, but it does mean nobody is fixing it, and a package nobody maintains is a poor thing to hand your connection string to. Use a maintained server instead: postgres-mcp is the one this page walks through, because it documents the access mode that matters.

If your database is a hosted Postgres with its own vendor server, use the vendor's. The rule below about read-only access applies the same way.

The command

With uv installed, uvx runs the package without a separate install step. Run this in your normal terminal, not inside a Claude Code session, then start a session:

claude mcp add postgres \
  --env DATABASE_URI=postgresql://user:password@localhost:5432/dbname \
  -- uvx postgres-mcp --access-mode=restricted

The server name comes first, then the environment flag, then the double dash. That order matters: Claude Code reads a word directly after --env as another KEY=value pair and refuses the command, so the name goes before the flag rather than between it and the double dash.

In Docker, the image is crystaldba/postgres-mcp and the connection string goes in as an environment variable:

claude mcp add postgres -- docker run -i --rm \
  -e DATABASE_URI=postgresql://user:password@localhost:5432/dbname \
  crystaldba/postgres-mcp --access-mode=restricted

The image remaps the hostname localhost so that a connection string written for a database on your machine still reaches it from inside the container.

If you would rather not use uvx at all, install the package with pipx install postgres-mcp (or uv pip install postgres-mcp) and end the add command with the installed binary instead of uvx postgres-mcp: claude mcp add postgres --env DATABASE_URI=postgresql://user:password@localhost:5432/dbname -- postgres-mcp --access-mode=restricted. For Docker, use the docker run line above.

The .mcp.json equivalent

{
  "mcpServers": {
    "postgres": {
      "command": "uvx",
      "args": ["postgres-mcp", "--access-mode=restricted"],
      "env": {
        "DATABASE_URI": "postgresql://user:password@localhost:5432/dbname"
      }
    }
  }
}

A .mcp.json at the repository root is project scope by definition, so never put a literal password in it: point DATABASE_URI at a variable the machine supplies, and keep a literal connection string at local or user scope instead, where it is stored in your own configuration.

Which scope to use

Local is the default and the right answer for most people: the server lives in your own configuration and applies to this project only. For another scope, add the flag to the same command: --scope user for every project, --scope project for the shared file described next. User scope makes one connection string available everywhere, which is convenient until you are in the wrong project and Claude queries the wrong database.

Project scope writes .mcp.json at the repository root and shares it with whoever clones the repository, so it is the right choice only for a database everyone on the team may read, reached with a credential everyone holds.

The auth step

There is no OAuth here: the server authenticates with the connection string, so the database user and its grants are the whole permission model. Create a user that can read the schemas Claude should see and nothing else, and let the read-only access mode cover the rest.

The server takes the connection information at startup rather than per query, so one fixed connection string covers the whole session: convenient when you always connect to the same database, and a deliberate step when you switch.

Check the string outside Claude Code first: the README suggests confirming your credentials with psql or a database client. If that login fails, no MCP configuration will rescue it.

Making it read-only

The access mode is the difference between a database assistant and a database incident. Restricted mode limits operations to read-only transactions and imposes constraints on resource utilisation, presently only execution time; unrestricted mode allows full read and write access to modify data and schema, and the README frames that as suitable for development.

Restricted mode does more than open a read-only transaction. The server parses the SQL before running it and rejects any statement containing commit or rollback, which is the obvious way to leave a read-only transaction.

The README is honest about the limit of that: if you have unsafe stored procedure languages enabled on your database, the read-only protections could be circumvented. So treat restricted mode as the second lock, and the database grants as the first.

Confirm it connected

claude mcp list
claude mcp get postgres

The list prints a health status beside each server, such as ✔ Connected, ! Needs authentication or ✘ Failed to connect. A local-scope Postgres server has no sign-in step, so a failed connection there means the process did not start, which in practice is the connection string or the missing package. A project-scoped server can instead read ⏸ Pending approval or ⊘ Disabled for this project, which report a configuration decision rather than a failed connection.

Inside a session, /mcp lists the tools. Their names follow mcp__postgres__<tool>, which is also how a permission rule names one. Then ask a real question about a table you know, and check the answer against what you expect.

The failure people hit

The most common one is not an error message at all: following an older tutorial installs the archived reference server, which works until it does not, and nobody is maintaining it. Check which package the command names before you run it.

The second is the access mode. The README's own configuration example uses unrestricted mode, so a copy and paste from it hands Claude write access to the database as well as read; change that one word to restricted unless you meant it.

The rest of the messages are in the MCP errors guide. The shared flags and scopes are in Add an MCP server to Claude Code.

Sources

Every line on this page rests on one of these. The date beside each one is the day we read it.

  • crystaldba/postgres-mcp README, for the installs, the DATABASE_URI variable, both access modes, the SQL parser that rejects commit and rollback, the startup connection, the Docker localhost remap, the psql check and the uvx JSON entry. Read November 25, 2026.
  • modelcontextprotocol/servers README, for the PostgreSQL reference server being listed under archived servers. Read November 25, 2026.
  • Anthropic: Connect Claude Code to tools via MCP, for the command shape, the --env pair rule, the double dash, the scope table, the health statuses and the tool-name pattern. Read November 25, 2026.

Common questions

Which Postgres MCP server should I use with Claude Code?

Use a maintained one such as postgres-mcp, which installs with uv, pipx or Docker and runs read-only when you ask it to. The PostgreSQL server that used to sit in the official reference list is in that repository's archived section now.

What is the command to add it?

claude mcp add postgres followed by an environment flag carrying DATABASE_URI, a double dash, and uvx postgres-mcp with its access mode. Everything after the double dash is the server's own command line.

How do I keep Claude from writing to my database?

Start the server in restricted mode. It then limits operations to read-only transactions and rejects any SQL containing commit or rollback, which closes the obvious way around a read-only transaction.

Can it connect to a database in Docker?

Yes, and the image remaps the hostname localhost so a connection string written for a database on your machine still points at it from inside the container.

Why do my queries fail when the server looks up?

The server takes its connection details at startup, so the connection string is fixed for the session rather than chosen per query. Check the same string with psql or a database client before blaming the MCP setup.

Every session, one window

Omniscio runs several agent sessions side by side in one window, each with its own context and its own diff view. Any model, any harness — Claude Code, Codex, OpenCode, Devin, Pi, and Hermes all supported.