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

# Repair ownership

> Repair object ownership for supported legacy imports. Managed databases with protected roles use their grants manifest instead.

Repairs object ownership for a supported legacy database after a self-managed import. The agent refuses this operation on a managed database with a protected SQL Explorer role; that database requires the managed grants manifest path.

## When to use this

Call this endpoint after you've imported a dump into a supported legacy database yourself
(see [Restore your own dump](https://docs.dbhost.app/guides/restore-your-own-dump.md)) and verified that an ownership mismatch causes the failure.

**Why it happens:** A dump can carry object owners from the source server
that differ from your DBHost owner role. Restores that DBHost runs from its
own backups already end with this repair step. Use this endpoint for:

* Clean-up after your own `psql` / `pg_restore` import.
* Databases restored before the automatic repair existed.
* A legacy import whose object owners differ from the database owner.

If a managed Runtime or Readonly role receives `permission denied`, check its explicit `GRANT` and default privileges in your migrations. Changing the owner role does not grant access to those app roles. DBHost managed restores use a separate manifest-backed repair path.

## Path parameters

| Parameter | Type | Description |
| - | - | - |
| `id` | uuid | Database ID |

## What it does

For a supported legacy target database only:

1. `ALTER SCHEMA public OWNER TO <owner>` + grants `USAGE, CREATE`.
2. Iterates every non-system schema and issues `ALTER TABLE/SEQUENCE/VIEW/MATERIALIZED VIEW ... OWNER TO <owner>`.
3. Restores read access for the dashboard SQL Explorer on the affected tables.

The operation is scoped to a single database. It cannot affect any other tenant's data.

## Response

```json 200 OK theme={null}
{
  "name": "mydb",
  "status": "repaired",
  "message": "Ownership repair completed for 'mydb'"
}
```

## Errors

| Status | Code | Description |
| - | - | - |
| 401 | `UNAUTHORIZED` | Missing or invalid API key |
| 404 | `DATABASE_NOT_FOUND` | Database doesn't exist, isn't owned by this user, or isn't in scope for this key |
| 409 | `OPERATION_BINDING_CONFLICT` | Protected managed ownership requires the grants manifest path |
| 500 | `AGENT_ERROR` | DBHost couldn't connect to the database or repair ownership |

## CLI

```bash theme={null}
dbhost databases repair-ownership <database-id>
```

> [!TIP]
>
> Running this when ownership is already correct is a no-op — every `ALTER ... OWNER TO` statement is idempotent.


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