> ## Documentation Index
> Fetch the complete documentation index at: https://midplane.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# The role script and the role check

> The script that gives Midplane a role of its own that can only read, what each of its statements does, and what the role check warns about.

Midplane only reads, whatever user it connects as. Its own role adds the
database's lock: reads only, few connections, a time limit. The source's
page makes the script, password included, and switches the source to the
role.

## Run it

You need a user that can create roles (Postgres) or users (MySQL).

1. On Postgres, untick any schema under **Schemas it reads** that Midplane
   shouldn't read. **Read past row-level security** comes ticked on
   Supabase; Midplane's own access rules still apply.
2. Select **Make the script**. It names the role (`midplane_` and eight
   letters and digits) and shows its password this once. **Make a new
   script** replaces the one waiting: run only the latest.
3. Run it once, as an admin, in the source's own database. For a read
   replica, run it on the primary.
4. Select **Switch to midplane\_…**. Midplane logs in as the role, checks
   it, and replaces the credentials it holds: it no longer keeps the user
   you first connected with.

## What the Postgres script does

One `DO` block, so it runs whole or not at all. Pasted in part, it is a
syntax error; run in another database, it stops with `Connect to the
database shop first: nothing was changed.`

| Statement | Effect |
| - | - |
| `CREATE ROLE … LOGIN PASSWORD … CONNECTION LIMIT 10` | Logs in, 10 connections at most. The password goes in as a SCRAM verifier, out of your logs (plain on Neon, which takes nothing else). Adds `BYPASSRLS` with **Read past row-level security**. |
| `SET default_transaction_read_only = on` | Every session is read-only. |
| `SET statement_timeout = '30s'` | A statement stops after 30 seconds. Midplane sets a tighter limit on its own. |
| `SET idle_in_transaction_session_timeout = '60s'` | An idle transaction closes after 60 seconds. |
| `GRANT CONNECT`, `USAGE`, `SELECT ON ALL TABLES` | Reads the tables and views of each schema ticked. |
| `ALTER DEFAULT PRIVILEGES … GRANT SELECT ON TABLES` | Tables made later are readable too, if you or an owner you can act for makes them. Others need a `GRANT SELECT` from their owner. |

## What the MySQL script does

| Statement | Effect |
| - | - |
| `CREATE USER … REQUIRE SSL WITH MAX_USER_CONNECTIONS 10 ACCOUNT LOCK` | TLS only, 10 connections at most, locked. |
| ``GRANT SELECT, SHOW VIEW ON `shop`.*`` | Reads every table and view of the source's database, later ones included. No `EXECUTE`: stored functions stay uncallable. |
| `ALTER USER … ACCOUNT UNLOCK` | Unlocks it last, so a partial run leaves no account that can log in. Run the whole script again. |

The password is plain in the script; MySQL hides it in its logs. MySQL has
no per-account `max_execution_time`, so Midplane sets it per connection.

## The role check

Every test and every switch checks what the user Midplane connects as can
do beyond reading. Midplane still only reads; a warning means the database
wouldn't stop it. Each names up to five objects.

| Engine | The role |
| - | - |
| Postgres | is a superuser |
| Postgres | owns tables or schemas, or is a member of a role that does, so it can alter or drop them |
| Postgres | can read or write the server's files or run programs (a `pg_*_server_*` role) |
| Postgres | can insert, update or delete in some tables |
| Postgres | can create tables in some schemas |
| MySQL | has privileges on every database (`ON *.*`) |
| MySQL | has privileges beyond SELECT and SHOW VIEW |
| MySQL | is granted roles, whose privileges Midplane can't see |

`BYPASSRLS` is not a warning. Any warning shows **Role can do more than
read** on **Sources**.

### Postgres 14 and older

Before Postgres 15, every role may create tables in `public`, so even
Midplane's role gets **can create tables in some schemas (public)**. To
clear it, revoke that from everyone, as Postgres 15 does, once you've
checked nothing of yours creates tables there as another user:

```sql theme={null}
REVOKE CREATE ON SCHEMA public FROM PUBLIC;
```

## When you cannot run the script

Some hosts let no user create roles: on Fly Managed Postgres the script
stops with a permission error and changes nothing. Connect with the host's
own read-only user instead, or paste its string under **Credentials** and
select **Test and replace**:

* Fly: a user with the Reader role ([Fly](https://midplane.ai/docs/hosts/fly.md)).
* PlanetScale Postgres: a dashboard role with `pg_read_all_data`
  ([PlanetScale](https://midplane.ai/docs/hosts/planetscale.md)).

Without the script you give up the role's connection and time limits, and
tables made later are readable only if your user's grants cover them.
Midplane still keeps its connections few and limits every statement.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.