Skip to content

Database token access for Trusted Partners

As a Trusted Partner, your OAuth app can connect to a customer's Postgres database with database token access. Supabase creates a custom Postgres role for your app in each project you set up. You connect as that role with your OAuth access token as the password. Customers never share their database password with you.

To see what your customers see and how they revoke your access, read OAuth apps with database token access.

How it works

  1. You add the Database token access permission to your OAuth app.
  2. A customer authorizes your OAuth integration for their organization.
  3. You call the setup endpoint for each project you want to reach. It creates your role and turns on database token access on the project.
  4. You connect to Postgres as that role with the current OAuth access token as the password.

Step 1 happens once. Step 2 happens once for each customer organization. Steps 3 and 4 happen for each project.

Requirements

ForRequirement
Your organizationApproved by Supabase as a Trusted Partner.
Your OAuth appOwned by your Trusted Partner organization. If your app uses scoped access, it works only with Who approves access? set to An owner, once for the whole organization.
The customer's projectPostgres 17.6.1.081 or later, active and healthy, and on a platform that supports SSL enforcement. Branches aren't supported.
Your connectionSSL, directly or through the shared pooler (Supavisor). The dedicated pooler (PgBouncer) isn't supported.

If you don't have an OAuth app, follow Build a Supabase OAuth integration.

Set up database token access

Step 1: Add database token access to your OAuth app

After Supabase approves your organization, open your OAuth app's settings in your organization's OAuth Apps tab. You can change OAuth app permissions only in the Supabase Dashboard because the Management API doesn't have an endpoint for them. Set Database token access to one of these levels:

Access levelScopeWhat your role can do
Read onlydatabase_token_access:readRead every table in every schema through the supabase_read_only_user role.
Read and writedatabase_token_access:writeEverything the postgres role can do. Your sessions act as postgres and can change data, create schemas and functions, and schedule jobs.

At both levels your role bypasses Row Level Security and has the pg_monitor role.

Your app also needs Database management with read access (database:read) to open connections and read the project's pooler and replica details. To list the customer's projects, add Projects with read access (projects:read).

By default, a customer authorizes your app for every project in their organization. Because database token access works with scoped access for OAuth apps, you can turn on project selection and each customer chooses which projects your app can reach. To turn it on, set Which projects can it reach? to Let them choose. You can't turn this setting off after you save it.

Customers who authorized your app before you added database token access must authorize it again. The same applies after you change the access level. Each customer has one authorization for your app. When a customer authorizes it again at a new level, the tokens you already hold keep working and take on the new level.

Step 2: Get the customer's authorization

Authorization uses the standard Supabase OAuth flow. On the consent screen the customer sees Database token access with your access level, a warning that your app bypasses their access rules, and the list of projects where setup will turn on SSL enforcement.

Without project selection, the authorization covers every project in the customer's organization, including projects created later. With project selection turned on, the customer chooses all current and future projects or up to 10 specific projects.

The customer can't authorize your app until they fix every project listed under These projects require changes. A project lands on that list when it doesn't meet the project requirements. A customer who selected specific projects can remove a listed project from the selection instead. Tell customers about these checks before you send them to the consent screen.

Authorization returns an access token and a refresh token. It doesn't change any project until you call setup.

Step 3: Set up each project

To find the customer's projects, call GET https://api.supabase.com/v1/projects with the access token. Each entry has the project's status and its Postgres version in database.version. Branches aren't included. If the customer selected specific projects, the list has only those projects.

Then call setup once for each project with the OAuth access token and no request body. Replace YOUR_OAUTH_ACCESS_TOKEN with the customer's access token and YOUR_PROJECT_REF with the project's ref:

export ACCESS_TOKEN="YOUR_OAUTH_ACCESS_TOKEN"
export PROJECT_REF="YOUR_PROJECT_REF"
curl -X POST "https://api.supabase.com/partners/database-access/projects/$PROJECT_REF" \
-H "Authorization: Bearer $ACCESS_TOKEN"

A successful call returns 200 with your role name:

{
"role": "example_partner_user_3f9a2c1e"
}

The role name combines your organization's name with the first eight characters of your OAuth app's client ID. Because of that, each of your OAuth apps gets its own role.

Setup makes these changes:

  • It creates your role and gives it your access level. If your app already set up the project, setup reuses the role.
  • It turns on database token access if it's off.
  • It turns on SSL enforcement if it's off.

When setup turns on either setting, connections through the shared pooler close and reconnect. Turning on SSL enforcement also closes non-SSL connections from public addresses and restarts the dedicated pooler if the project runs one. Tell customers in your UI that their other connections must use SSL to keep working.

Setup is safe to repeat. Call it after every authorization because it also applies a changed access level.

Step 4: Connect

Connect over SSL as your role with the current OAuth access token as the password. To verify the server certificate with sslmode=verify-full, download the Supabase CA certificate. The same certificate works for every project and for the shared pooler.

Connect directly

Replace YOUR_ROLE with the role name that setup returned:

export ROLE="YOUR_ROLE"
PGPASSWORD="$ACCESS_TOKEN" psql \
"postgresql://db.$PROJECT_REF.supabase.co:5432/postgres?user=$ROLE&sslmode=verify-full&sslrootcert=prod-ca-2021.crt"

Direct connections use IPv6 unless the project has the IPv4 add-on. If your network doesn't support IPv6, connect through the shared pooler.

Connect through the shared pooler

A shared pooler connection string has this shape:

postgresql://YOUR_POOLER_HOST:YOUR_PORT/postgres?user=YOUR_ROLE.YOUR_PROJECT_REF&sslmode=verify-full&sslrootcert=prod-ca-2021.crt&options=-c%20jit%3Dtrue
PartValue
HostThe db_host value from GET https://api.supabase.com/v1/projects/YOUR_PROJECT_REF/config/database/pooler.
Port6543 for transaction mode or 5432 for session mode.
UserYour role name, a dot, and the project ref.
Optionsjit=true. The pooler reads this option to choose token authentication and doesn't pass it to Postgres. Leave it off direct connections.
SSLRequired.

Because a direct connection holds a database connection while Supabase checks the token, use the shared pooler for many short-lived connections.

Connect to a read replica

A read replica accepts your role and the same access token without another setup call. It's read-only at either access level:

postgresql://db.YOUR_REPLICA_ID.supabase.co:5432/postgres?user=YOUR_ROLE&sslmode=verify-full&sslrootcert=prod-ca-2021.crt

To find a project's replicas, call the pooler endpoint above and read the identifier of each entry whose database_type is READ_REPLICA.

Set session defaults

Your sessions use the project's settings. The default search_path leaves out the extensions schema where pg_stat_statements lives.

With read-only access, set defaults on your own role once after setup. They apply when a database connection opens and survive later setup calls:

alter role current_user set search_path = "$user", public, extensions;
alter role current_user set statement_timeout = '3s';

Because the shared pooler's transaction mode reuses open database connections, a new default reaches your sessions only as the pooler opens new ones.

With read and write access, each session switches to the postgres role as soon as it connects. Running alter role current_user in that session changes the customer's postgres role rather than yours. Set the values at the start of each session instead:

set search_path = "$user", public, extensions;
set statement_timeout = '3s';

In the shared pooler's transaction mode, a set lasts only until the transaction ends. Use set local inside each transaction or connect in session mode.

Keep connections working

The access token expires after 24 hours. Because Supabase checks it only when a connection opens, an open connection keeps working after the token expires whereas each new connection needs the current token.

Refresh the token before it expires. Each refresh returns a new refresh token and invalidates the old one right away. Run one refresh at a time for each customer and give each refresh request enough time to finish. If a refresh succeeds but you lose the response, the old refresh token no longer works and the customer must authorize your integration again.

When a connection fails:

  1. Check the project with GET https://api.supabase.com/v1/projects/YOUR_PROJECT_REF. A paused or restoring project refuses connections.
  2. If the access token expired, refresh it and reconnect with backoff.
  3. If the refresh returns 404 with No such refresh token found, treat the customer as disconnected. Stop retrying and ask the customer to authorize your integration again. After a timeout or a 5xx response, keep the current refresh token and retry with backoff.

Troubleshooting

Setup errors

Error responses have a message field and no error code. Several cases share a status. Read the message to tell them apart.

StatusMessageWhat to do
400Invalid project ref: <ref>Check the project ref.
401Invalid access tokenRefresh the access token.
401JWT could not be decodedThe Authorization header doesn't hold a valid token. Send Bearer followed by the OAuth access token.
403OAuth app is not a Trusted Partner appContact Supabase.
403Authorization is not organization-boundYour app uses scoped access with member permissions. Set Who approves access? to An owner, once for the whole organization.
403Authorization has no database accessThe customer's authorization lacks database token access or database:read. Ask the customer to authorize your integration again.
403Database role setup requires an OAuth access tokenYou called setup with a personal access token. Use the OAuth access token from the customer's authorization.
403Authorization does not cover this projectThe customer selected specific projects and left this one out. Ask the customer to authorize your integration again and select it.
403ForbiddenThe project isn't in the organization the customer authorized.
404Not FoundCheck the project ref.
409Database role setup is not supported for branch projectsUse the main project's ref.
409Project is unavailable for database role setupThe project is paused, restoring, unhealthy, or idle. Retry later.
409Database role setup is not supported on this project platformContact Supabase.
409Postgres 17 or later is required for database role setupAsk the customer to upgrade the project.
409Database role conflicts with an existing role: ...A role with your role name exists that Supabase didn't create for your app. Ask the customer to rename or drop it.
503Database role setup is temporarily unavailable; retry laterAnother setup call for this app and project is running or the database is briefly unavailable. Retry with backoff.

Connection errors

password authentication failed

Refresh the access token and check that you connect as the role setup returned.

unsupported or invalid secret format

The full message starts with (EAUTHQUERY). Without jit=true in your shared pooler connection, the pooler looks for a password your role doesn't have. Add jit=true to options.

SSL connection is required for user

Connect with sslmode=verify-full.

failed to reach JIT provider

Your app opened too many token connections to the project in a short time. Wait a minute before you retry. For the limit, see failed to reach JIT provider for user.

too many authentication failures

The shared pooler blocked your address after repeated failed attempts. The full message ends with new connections are temporarily blocked. Fix the cause of the failures before you retry. For how long the block lasts, see too many authentication failures.

Refresh fails with No such refresh token found

The refresh request returns 404. Another request already used the refresh token or the customer revoked your integration. Run one refresh at a time. If it keeps failing, treat the customer as disconnected.

pg_stat_statements fails with relation does not exist

Add the extensions schema to your search_path.

A monitoring extension is missing

A read-only role can't create extensions such as pg_buffercache. Ask the customer to turn the extension on under Database Extensions.

JIT compilation turns on for your session

You added jit=true to a direct connection. Remove it.

A session setting is gone in the next transaction

The shared pooler's transaction mode doesn't keep session settings. Use set local inside each transaction or connect in session mode.

Replication

Your role can't open a replication connection. With read and write access you can create a logical replication slot over a normal connection and read it with pg_logical_slot_get_changes. Read your slot on a schedule and drop it when you stop, because an unread slot fills the customer's disk.

When access ends

Your access to a customer's projects ends when the customer revokes your integration, when you revoke the customer's authorization, or when Supabase withdraws your Trusted Partner status. Supabase refuses your new connections right away. It then turns off login for your role and closes your open connections on the project and its read replicas. Supabase doesn't notify you. You find out when a refresh fails with No such refresh token found.

The role stays in the database with login turned off. If the customer authorizes your integration again, your next setup call turns login back on. If they authorize read-only access after you had read and write access, the next setup call downgrades the role and closes your open connections.

Everything you created stays in the database and keeps running after access ends. Give your customers a list of what your integration creates and the SQL that removes each item.

To end a customer's authorization yourself, call POST /v1/oauth/revoke with your client_id, your client_secret, and a refresh_token you hold for that customer. Revoking ends your access to every project in that organization. Before you revoke, remove the triggers, functions, pg_cron jobs, and replication slots you created.

Move existing customers from a password

If your integration connects today with a database password or a role the customer created for you, move each customer in this order:

  1. Take the customer through Step 2 to Step 4 with your updated OAuth app.
  2. Confirm the new connections work.
  3. Ask the customer to drop the old role or reset the database password.

If the old role has the same name as your new role, setup returns 409 until the customer renames or drops it.