User Management

      The Ruby SDK lets you create users, assign them roles and associated privileges, and remove them from the system.

      User-Management APIs

      Users who have been assigned the Admin role for the cluster are able to create, edit, and remove users. The Ruby SDK provides APIs to support these activities. A high-level summary of the APIs can be found in User-Management, and details of all options in the UserManager API docs.

      Using the UserManager API

      The most common uses of the UserManager API are creating and listing users:

      Creating Users
      user =
      user.username = "testUsername"
      user.password = "testPassword"
      user.display_name = "Constance Lambert"
      user.roles = [
        # Roles required for the reading of data from the bucket do |r|
 = "data_reader"
          r.bucket = "*"
        end, do |r|
 = "query_select"
          r.bucket = "*"
        # Roles required for the writing of data into the bucket do |r|
 = "data_writer"
          r.bucket = "*"
        end, do |r|
 = "query_insert"
          r.bucket = "*"
        end, do |r|
 = "query_delete"
          r.bucket = "*"
        # Role required for the creation of indexes on the bucket do |r|
 = "query_manage_index"
          r.bucket = "*"
      Listing Users
      list_of_users = cluster.users.get_all_users
      list_of_users.each do |current_user|
        puts "User's display name is: #{current_user.display_name}"
        current_roles = current_user.roles
        current_roles.each do |role|
          puts "    User has the role: #{}, applicable to bucket #{role.bucket}"

      Using a user created in the SDK to access data:

      bucket_name = "travel-sample"
      options =
      options.authenticate("testUsername", "testPassword")
      user_cluster = Cluster.connect("couchbase://localhost", options)
      user_bucket = user_cluster.bucket(bucket_name)
      user_collection = user_bucket.default_collection
      options =
      options.ignore_if_exists = true
      cluster.query_indexes.create_primary_index(bucket_name, options)
      p returned_airline10_doc: user_collection.get("airline_10").content
      airline11_object = {
        "callsign" => "MILE-AIR",
        "iata" => "Q5",
        "icao" => "MLA",
        "id" => 11,
        "name" => "40-Mile Air",
        "type" => "airline",
      user_collection.upsert("airline_11", airline11_object)
      p returned_airline11_doc: user_collection.get("airline_11").content
      result = user_cluster.query("SELECT * from `#{bucket_name}` LIMIT 5")
      result.rows.each_with_index do |row, idx|
        puts "#{idx}: #{row}"

      Further Reading

      The SDK also contains management APIs for dealing with Cluster resources.