CockroachDB Create User

CockroachDB create user

Creating a new user is a common task in database administration. If you’re using CockroachDB to store and manage data, it’s important to know how to add a new user to the system. Fortunately, the process is a simple one. In this article, we’ll provide step-by-step instructions for using CockroachDB to create a user.

Prerequisites

Before proceeding with the instructions in this tutorial, make sure that CockroachDB is already installed on your machine. If you’re not sure whether it’s installed, use the command cockroach version. The output of this command will show the tag number and version of CockroachDB on your system.

Start a single node cluster

Our first step will be to create or start a single-node cluster in CockroachDB. Although it’s possible to set up a multi-node cluster, a single node will be sufficient for the purposes of this tutorial. The basic command used to start the cluster looks like the following:

1
cockroach start-single-node

Next, we’ll set the flags that we need to use with this command. In this tutorial, we will be using several flags:

  • --insecure – This flag indicates insecure access where there is no authentication or encryption. With this type of access, anyone can read and write the data in the cluster. There are no passwords required in insecure mode, even for the root superuser.

  • --store – This flag provides the path to a storage device. If your node makes use of multiple storage devices, you’ll have to specify each one with a separate --store flag.

  • --listen-addr – This flag indicates the hostname or IP address as well as the port to listen for client connections. The default IP address in CockroachDB is localhost:26257.

  • --http-addr – This flag indicates the hostname or IP address as well as the port to listen for HTTP requests. An example would be localhost:8080.

  • --background – This flag is used to start the node in the background. Using the --background flag is preferable to using & in conjunction with the command. This is because the flag ensures that the shell only regains control when the node is prepared to accept requests.

To create our single-node cluster, we’ll use the following command:

1
2
3
4
5
6
linux@linux:~$ cockroach start-single-node \
--insecure \
--store=node1 \
--listen-addr=localhost:26257 \
--http-addr=localhost:8080 \
--background

Once the command is executed, it will let you know that you’re running in insecure mode.

Access the SQL shell

Next, we’ll open the SQL shell in CockroachDB using the following command:

1
cockroach SQL --insecure --host=localhost:26257

Now that we’re successfully connected to the SQL shell, we’re able to execute various SQL commands. We’re ready to try creating a new user in CockroachDB.

Create user in CockroachDB

In CockroachDB, there are a few restrictions on usernames that you need to keep in mind when creating a new user:

  • Usernames are case sensitive.
  • The username must start with a letter or underscore.
  • The username must contain only numbers, underscores and letters.
  • The username must be between 1 to 63 characters.

To create a user in CockroachDB, use the command shown below:

1
2
3
4
root@localhost:26257/defaultdb> CREATE USER objectrocket;
CREATE USER 1

TIME: 139.963569ms

In this example, we created a new user named objectrocket.

NOTE: Keep in mind that we created this user in insecure mode. If we wanted to create a user in secure mode, we’d also need to create security certificates. In addition, password creation can only take place in secure clusters, so it’s important to make sure you’re running in secure mode if you want to create a user with a password.

Conclusion

When you’re working with CockroachDB, it’s important to know how to create users on your system. Creating users gives database administrators the ability to grant privileges and control access to different areas of the database. In this article, we walked through the process of starting up a single-node cluster and then using CockroachDB to create a new user. With these instructions to guide you, you’ll be able to set up new users on your own CockroachDB implementation.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.