Short answer: ChatGPT can't open a connection to PostgreSQL by itself. Give it one through an app that only runs read-only SQL. With AI2SQL you add the database once with a read-only role, install the AI2SQL app in ChatGPT, and sign in. The same steps work for Supabase, Neon, Amazon RDS and Azure Database for PostgreSQL.
Step 1: a read-only PostgreSQL role
Enforce read-only in PostgreSQL itself, so a write fails even if every other layer missed it:
CREATE ROLE chatgpt_ro LOGIN PASSWORD 'a-long-random-password';
GRANT CONNECT ON DATABASE appdb TO chatgpt_ro;
GRANT USAGE ON SCHEMA public TO chatgpt_ro;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO chatgpt_ro;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO chatgpt_ro;
ALTER ROLE chatgpt_ro SET default_transaction_read_only = on;
To keep some tables out of reach, grant SELECT on chosen tables or a separate schema
instead of ALL TABLES.
Step 2: add the database to AI2SQL
Sign in at builder.ai2sql.io, open Connections, pick
PostgreSQL and enter the host, port (usually 5432), database and the
chatgpt_ro role. On Supabase or Neon, copy the host and port from the project's
connection settings. If PostgreSQL runs on your laptop or inside a private network, use the
desktop connector instead.
Step 3: install AI2SQL in ChatGPT
- Open the AI2SQL app in the ChatGPT plugin directory and add it.
- ChatGPT opens AI2SQL. Sign in with the account that holds the PostgreSQL connection and press Allow.
- In a new chat, type @AI2SQL so ChatGPT uses the app for your question.
Prefer to add it by hand? Turn on developer mode in ChatGPT's connector settings and create a
connector with the URL https://builder.ai2sql.io/api/chatgpt-mcp and OAuth authentication.
Step 4: ask a question
Try "@AI2SQL describe my Postgres database, then show monthly revenue for the last 6 months."
ChatGPT reads the real schema with describe_schema, writes PostgreSQL SQL
(date_trunc, ILIKE, ::date casts) and runs it with
run_query. Ask ChatGPT to show the SQL it ran if you want to check it.
What keeps it safe
- Database level: the
chatgpt_rorole can only read, and its transactions default to read-only. - Gateway level: one
SELECTorWITHstatement per call; writes,COPY, DDL and functions such aspg_read_file,dblinkandpg_sleepare rejected before they reach PostgreSQL. - Limits: results are capped at 1,000 rows.
- No shared secrets: ChatGPT holds a revocable sign-in token, never the database password.
- Audit: every call appears in Agent activity with the SQL, the database and the result, including blocked attempts.
Common PostgreSQL snags
- Connection refused or timed out: the database only accepts connections from certain IPs. Allow AI2SQL in the firewall or security group, or use the desktop connector for private networks.
- No pg_hba.conf entry: self-hosted servers need a
hostline for thechatgpt_rorole with password authentication. - Empty schema: the role has no
USAGEon the schema or noSELECTon its tables. Re-run the grants in the right database.
The same connection also works from Claude and Cursor. For MySQL and SQL Server, see ChatGPT + MySQL and ChatGPT + SQL Server.