How to Manage PostgreSQL Users and Databases in a Database Cluster

PostgreSQL is an open source, object-relational database built with a focus on extensibility, data integrity, and speed. Its concurrency support makes it fully ACID-compliant, and it supports dynamic loading and catalog-driven operations to let users customize its data types, functions, and more.


PostgreSQL database clusters come configured with a default database (defaultdb) and a default administrative user (doadmin). These defaults are necessary for cluster replication and administration, so you can’t delete them, but you can add additional users and databases.

By default, every database cluster is publicly accessible. To limit access, you can add trusted sources or manage user permissions.

Tip
If you connect to the database cluster with preconfigured connection details from the cluster’s Overview page, you can edit them to use a different user and password or a different database.

Add or Delete a Database User Using the CLI

How to create a database user using the DigitalOcean CLI

To create a database user via the command-line, follow these steps:

  1. Install doctl, the DigitalOcean command-line tool.

  2. Create a personal access token, and save it for use with doctl.

  3. Use the token to grant doctl access to your DigitalOcean account.

                  doctl auth init
                
  4. Finally, create a database user with doctl databases user create. The basic usage looks like this, but you'll want to read the usage docs for more details:

                  doctl databases user create <database-cluster-id> <user-name> [flags]
                
How to delete a database user using the DigitalOcean CLI

To delete a database user via the command-line, follow these steps:

  1. Install doctl, the DigitalOcean command-line tool.

  2. Create a personal access token, and save it for use with doctl.

  3. Use the token to grant doctl access to your DigitalOcean account.

                  doctl auth init
                
  4. Finally, delete a database user with doctl databases user delete. The basic usage looks like this, but you'll want to read the usage docs for more details:

                  doctl databases user delete <database-cluster-id> <user-id> [flags]
                

Add or Delete a Database User Using the API

How to create a database user using the DigitalOcean API

To create a database user using the DigitalOcean API, follow these steps:

  1. Create a personal access token, and save it for use with the API.

  2. Send a POST request to https://api.digitalocean.com/v2/databases/{database_cluster_uuid}/users

    cURL

    To create a database user with cURL, call:

    
                    curl -X POST \
      -H "Content-Type: application/json" \
      -H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \
      -d '{"name": "app-01"}' \
      "https://api.digitalocean.com/v2/databases/9cc10173-e9ea-4176-9dbc-a4cee4c4ff30/users"

    Go

    Go developers can use Godo, the official DigitalOcean V2 API client for Go. To create a database user with Godo, use the following code:

    
                    import (
        "context"
        "os"
    
        "github.com/digitalocean/godo"
    )
    
    func main() {
        token := os.Getenv("DIGITALOCEAN_TOKEN")
    
        client := godo.NewFromToken(token)
        ctx := context.TODO()
    
        addUserRequest := &godo.DatabaseCreateUserRequest{
            Name: "app-01",
        }
    
        user, _, err := client.Databases.CreateUser(ctx, "88055188-9e54-4f21-ab11-8a918ed79ee2", addUserRequest)
    
    }

    Python

    
                    import os
    from pydo import Client
    
    client = Client(token=os.environ.get("DIGITALOCEAN_TOKEN"))
    
    add_user_resp = client.databases.add_user(database_cluster_uuid="ab7bb7a", body={"name": "app-01"})
How to delete a database user using the DigitalOcean API

To delete a database user using the DigitalOcean API, follow these steps:

  1. Create a personal access token, and save it for use with the API.

  2. Send a DELETE request to https://api.digitalocean.com/v2/databases/{database_cluster_uuid}/users/{username}

    cURL

    To delete a database user with cURL, call:

    
                    curl -X DELETE \
      -H "Content-Type: application/json" \
      -H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \
      "https://api.digitalocean.com/v2/databases/9cc10173-e9ea-4176-9dbc-a4cee4c4ff30/users/app-01"

    Go

    Go developers can use Godo, the official DigitalOcean V2 API client for Go. To delete a database user with Godo, use the following code:

    
                    import (
        "context"
        "os"
    
        "github.com/digitalocean/godo"
    )
    
    func main() {
        token := os.Getenv("DIGITALOCEAN_TOKEN")
    
        client := godo.NewFromToken(token)
        ctx := context.TODO()
    
        _, err := client.Databases.DeleteUser(ctx, "9cc10173-e9ea-4176-9dbc-a4cee4c4ff30", "app-01")
    }

    Python

    
                    import os
    from pydo import Client
    
    client = Client(token=os.environ.get("DIGITALOCEAN_TOKEN"))
    
    delete_resp = client.databases.delete_user(database_cluster_uuid="aba134a", username="backend_user1")

Add or Delete a Database User Using the Control Panel

To add or delete users or databases to a database cluster, click the name of the database to go to its Overview page, then select the Users & Databases tab.

Screenshot of Users and Databases screen

Create a new database in the Databases section by entering a name in the Add new database field and clicking Save. You can also delete a database here by opening its More menu, clicking Delete, and then confirming the deletion.

Create new users in the Users section by entering a name in the Add new user field and clicking Save. New users receive minimal permissions by default. You can then add user privileges using third-party tools like psql. However, you cannot grant users some superuser permissions.

You can also delete a user here by opening the user’s More menu, clicking Delete, and then confirming the deletion. Similarly, to reset a user’s password, open the user’s More menu and select Reset password.