Connect Claude to PostgreSQL with an MCP server
You can ask Claude questions about the data in a PostgreSQL database and get answers from the live tables, without exporting anything or writing SQL. This guide shows how to set it up with Ailimu, a self-hosted MCP server, and why the database, not the software, keeps the final say over what Claude can reach.
If you are new to the idea, start with what an MCP server is. The same server also connects to FileMaker, and one install can hold both side by side.
What you need
- A PostgreSQL database that the machine running Ailimu can reach.
- Ailimu running as one Docker container. The install guide takes you through it.
- Claude, either Claude Desktop or Claude Code.
1. Give Ailimu an account of its own
This is the step that matters most. The database's permissions are the real limit on what Claude can do. Ailimu offers only what you tick in its wizard, but an account that cannot read or change something stops it however the wizard is set. So create a dedicated account and grant it only what Claude should have.
For reading, grant SELECT on the tables you plan to expose, or on a whole schema:
CREATE ROLE ailimu_ro LOGIN PASSWORD 'choose-a-long-password';
GRANT CONNECT ON DATABASE shop TO ailimu_ro;
GRANT USAGE ON SCHEMA public TO ailimu_ro;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO ailimu_ro;
Start here, with reading only. If you later want Claude to change a record, grant
UPDATE on the specific columns it may change, never on the whole table:
GRANT UPDATE (status, notes) ON orders TO ailimu_rw;
PostgreSQL itself then refuses a change to any other column, even if someone ticks it as writable in the wizard. If you use row-level security, it applies to this account like any other.
2. Put the login in .env
Add the account to the .env file beside Ailimu, then restart the container:
PG_USER=ailimu_ro
PG_PASSWORD=choose-a-long-password
The login is read from the environment only. It is never written to Ailimu's configuration file or to a backup.
3. Connect
- Open the wizard and choose New connection.
- For Connect to, choose PostgreSQL.
- Enter the host, the port (5432 unless you changed it), the database and the
schema (
publicunless your tables live elsewhere). One connection serves one schema; make another connection for a second schema. - Click Test connection.
A few details save time:
- Encryption defaults to encrypted, which is right for a server you reach over a network. Choose Off only for a database on the same machine or a network you trust. Encrypted, and check the server's certificate also verifies the server's identity.
- Docker and
localhost. Inside a container,localhostis the container, not your computer. To reach a database on the same machine, usehost.docker.internalon Docker Desktop for Mac or Windows.
4. Choose what Claude can see
Ailimu reads PostgreSQL's own catalog, so it knows every table, column type, primary key, foreign key and comment exactly. Nothing about keys or relationships is guessed, and the relationship diagram draws the keys the database declares.
Tick the tables Claude may use, then the fields. For each field you decide whether it can be returned, searched, or neither. Leave out anything the questions do not need, because the rows Claude reads are sent to the AI provider as part of the conversation. Then publish. Ailimu is read-only until you turn changes on.
Every value Claude supplies is sent to the database as a bound value, never written into a query.
5. Connect Claude
The last step of the wizard, Connect Claude, shows the address and token for this connection, and makes a one-click extension for Claude Desktop. The steps are the same as for FileMaker: see Connect Claude to your FileMaker data, from the part where Claude is connected.
Then ask a question you already know the answer to: "Which customers placed more than three orders last month?" Claude finds the right tables and answers with the rows it used.
What is different from FileMaker
- Views and materialised views appear as tables, and are read-only in practice.
- Text matching: a plain value matches anywhere in the text and ignores case. A
value starting with
=matches exactly. - Functions instead of scripts. A function declared
STABLEorIMMUTABLEcannot change data, which PostgreSQL itself enforces, so Ailimu marks it Cannot change data. AVOLATILEfunction can change data, so read it before you expose it. As with scripts, functions are offered only while read-only is off. - Large tables. On a table of more than 10,000 rows, Ailimu only sorts by the fields you ticked Search on. Tick it on columns that have an index.
- Time limit. PostgreSQL stops a statement after 30 seconds. A setting changes that.