Skip to content

Let your AI read your Postgres database, safely (read-only)

Level 3: Connect AI to your tools. About 8 min. Checked on 19 November 2026 against PostgreSQL 18 documentation.
Stage 3 of 4: Connect AI to your tools

In this lesson you'll connect an assistant to a Postgres database through a role that can only read, and keep production data out of reach.

"How many customers signed up last week, by plan?" You know the answer is in Postgres; you just don't want to write the SQL. With a database MCP server your assistant can look at the schema, write the query, run it and explain the result. The question is how to let it read without ever letting it write, drop or leak.

The answer doesn't depend on which server you pick: the database decides what the assistant can do, through the role you give it.

1. Use a copy, not production

Point the assistant at a read replica, a nightly copy or a staging database. If something goes wrong, nothing that customers use is touched.

2. Create a role that can only read

Connect as an admin and create a role for the assistant:

CREATE ROLE ai_reader LOGIN PASSWORD 'choose-a-long-random-password';
GRANT USAGE ON SCHEMA public TO ai_reader;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO ai_reader;

GRANT SELECT ON ALL TABLES IN SCHEMA covers the tables, views and foreign tables that exist now; tables you create later need their own grant. Leave out schemas or tables with personal data, or grant only the columns the assistant needs: PostgreSQL lets you grant SELECT per column.

3. Pick a server you've checked

Database servers in the directory, with their status and install config on each page:

MCP servers for databases from the directory, in the order of our category ranking (adoption, recent activity, reliability).

More for databases
  1. MCP server letting AI agents read and build Baserow databases, automations and dashboards

    • Remote
    • Freemium
    • Databases
  2. Postgres gateway for AI agents with table grants, column masking and approval for writes

    Runs locally
    • Open source
    • 5 GitHub stars
    • Databases
  3. MCP server letting AI assistants read, edit and export SchemaSketch DBML database diagrams

    • 15 tools
    • Remote
    • Free
    • Databases
  4. Read-only MCP server letting coding agents inspect Huawei GaussDB databases safely

    Not checked yet
    • 5 tools
    • Open source
    • 1 GitHub star
    • Databases

Before you choose, go through Is this MCP server safe?. For a database server look especially for: tools you can read and understand, a read-only mode if it has one, and an active repository. A tool marked readOnlyHint is a useful signal, but the MCP project is clear that annotations are hints, not guarantees. Your read-only role is the guarantee.

4. Connect it with the read-only role

Give the server a connection string for ai_reader, never for your admin user, and keep it in an environment variable or your app's secret input rather than in a committed file. Then add the server to your assistant the way the Claude Code, Cursor or VS Code lessons show.

In short

  • The database role, not the server, is what keeps the assistant read-only.
  • Use a copy of the data, and grant only the schemas and columns needed.
  • Check the server like any other, and keep its credentials out of files.

What to try next

Share this lesson
  • Find a database server for your stack: browse MCP servers and filter by category.
  • Practise the whole flow on a server that is read-only by design: connect ours.

Lesson 13 of 13 in this path

Words used in this lesson

Sources

Official documentation, checked on 19 November 2026. Products change: if a step differs, the source has the current version.

Get the next lesson

New Academy lessons are announced in the weekly AI-Powered email.