0

Update a Database User

by
Published Dec 20, 2024

To update an existing database user, send a PUT request to `/v2/databases/$DATABASE_ID/users/$USERNAME` with the desired settings. **Note**: only `settings` can be updated via this type of request. If you wish to change the name of a user, you must recreate a new user. The response will be a JSON object with a key called `user`. The value of this will be an object that contains the name of the update database user, along with the `settings` object that has been updated.

Script digitalocean Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Digitalocean = {
3
  token: string;
4
};
5
/**
6
 * Update a Database User
7
 * To update an existing database user, send a PUT request to `/v2/databases/$DATABASE_ID/users/$USERNAME`
8
with the desired settings.
9

10
**Note**: only `settings` can be updated via this type of request. If you wish to change the name of a user,
11
you must recreate a new user.
12

13
The response will be a JSON object with a key called `user`. The value of this will be an
14
object that contains the name of the update database user, along with the `settings` object that
15
has been updated.
16

17
 */
18
export async function main(
19
  auth: Digitalocean,
20
  database_cluster_uuid: string,
21
  username: string,
22
  body: {
23
    settings?: {
24
      pg_allow_replication?: false | true;
25
      opensearch_acl?: {
26
        index?: string;
27
        permission?: "deny" | "admin" | "read" | "readwrite" | "write";
28
      }[];
29
      acl?: {
30
        id?: string;
31
        topic: string;
32
        permission: "admin" | "consume" | "produce" | "produceconsume";
33
      }[];
34
    };
35
  },
36
) {
37
  const url = new URL(
38
    `https://api.digitalocean.com/v2/databases/${database_cluster_uuid}/users/${username}`,
39
  );
40

41
  const response = await fetch(url, {
42
    method: "PUT",
43
    headers: {
44
      "Content-Type": "application/json",
45
      Authorization: "Bearer " + auth.token,
46
    },
47
    body: JSON.stringify(body),
48
  });
49
  if (!response.ok) {
50
    const text = await response.text();
51
    throw new Error(`${response.status} ${text}`);
52
  }
53
  return await response.json();
54
}
55