YugabyteDB is a PostgreSQL-compatible open-source distributed SQL database. It adds horizontal scalability to applications built for PostgreSQL while keeping familiar relational database properties (SQL, strong consistency, and ACID transactions) along with distributed-system features (auto-sharding and fault tolerance).
Because YugabyteDB is PostgreSQL-compatible (YSQL), many tools that work with PostgreSQL work with YugabyteDB as well (for example psql or pgAdmin).
YugabyteDB is versatile when it comes to data and traffic volumes. Because it provides auto-scaling, auto-sharding, and auto-balancing, you won’t have to rearchitect your system the moment it becomes too successful for the initial architecture to cope with.
It is the best fit for cloud-native OLTP (i.e. real-time, business-critical) applications that need absolute data correctness and requires at least one of the following: scalability, high tolerance to failures, and globally-distributed deployments.
YugabyteDB also supports a Cassandra-compatible API (YCQL), which can be useful for workloads that previously relied on Cassandra-style data access patterns.
In this guide, we will run a single-node YugabyteDB cluster on a local machine using Docker and Docker Compose.
Related content:
- How to Run Postgresql 14 with Docker and Docker-Compose
- How to run PGAdmin 4 Using Docker and Docker-Compose
Prerequisites
Before proceeding, ensure that you have Docker installed on your machine. To download and install Docker, check out the installation page.
Running YugabyteDB with Docker
To create a 1-node cluster with a replication factor (RF) of 1, first pull the Docker image (pinning a tag is recommended so your guide stays reproducible):
| |
Once the image is downloaded, create a new container using the following docker run command:
| |
If you are running macOS (AirPlay Receiver enabled), port
7000may already be in use. In that case, replace-p 7000:7000with-p 7001:7000and access the master UI athttp://localhost:7001.
Below is a breakdown of the options:
-d: The detach option runs the container as a background process and displays the container ID. This option is needed to regain control of the shell since the yugabyted process is intended to be long-lived.--name yugabyte: This option gives the container a user-friendly name that can be used later.-p 7000:7000 -p 9000:9000 -p 5433:5433 -p 9042:9042: These options expose internal ports to the host so they can be interacted with from outside the container. These are YugabyteDB significant ports and will be discussed later.yugabytedb/yugabyte:2.18.0.1-b4: This is the container image and version (tag) to run.bin/yugabyted start --daemon=false: This command starts yugabyted, the parent process for YugabyteDB and passes additional options to set the base directory for the YugabyteDB data folder and directs the process to not run in the background (the default behavior which would cause the container to stop).
It is important to note that YugabyteDB is a distributed SQL database and that the image used is only a single node deployment (i.e. a replication factor of 1). This is not typical for a production environment which would usually be RF=3 or even RF=5. Running a multi-node environment locally is possible but beyond the scope of this guide.
Run the following command to check the cluster status:
| |
Check logs
| |
Ports and endpoints
These are the common ports you’ll use locally:
- YSQL (PostgreSQL-compatible):
localhost:5433 - YCQL (Cassandra-compatible):
localhost:9042 - YB-Master UI:
http://localhost:7000 - YB-TServer UI:
http://localhost:9000
Connecting to YugabyteDB (YSQL)
Inside the container, you can open the YSQL shell (ysqlsh):
| |
From your host, you can also connect using psql:
| |
The default database user in a local YugabyteDB container is commonly yugabyte. If you changed users/passwords, use your own values.
Connecting to YugabyteDB (YCQL)
To open the YCQL shell (ycqlsh) inside the container:
| |
Run YugabyteDB with Docker in a persistent volume
In the preceding docker run command, the data stored in YugabyteDB does not persist across container restarts. To make YugabyteDB persist data across restarts, you can add a volume mount option to the docker run command, as follows:
Create a ~/yb_data directory by executing the following command:
| |
Run Docker with the volume mount option by executing the following command:
| |
Using Docker Compose to run YugabyteDB
Using a Docker Compose file is often easier than managing multiple docker run options manually, and it makes persistence and port mappings explicit.
Save this as docker-compose.yaml
| |
To run, use the following command:
| |
Confirm that it is running as expected:
| |
Connecting to the Admin UI
The cluster you have created consists of two processes: YB-Master which keeps track of various metadata (list of tables, users, roles, permissions, and so on) and YB-TServer which is responsible for the actual end user requests for data updates and queries.
Each of the processes exposes its own Admin UI that can be used to check the status of the corresponding process, as well as perform certain administrative operations. The yb-master Admin UI is available at http://localhost:7000 and the yb-tserver Admin UI is available at http://localhost:9000. To avoid port conflicts, you should make sure other processes on your machine do not have these ports mapped to localhost.
Connect to the Yugabyte database
Using the YugabyteDB SQL shell, ysqlsh, you can connect to your cluster and interact with it using distributed SQL. ysqlsh is installed in the container and is located in the YugabyteDB bin directory.
| |
You can also connect to YugabyteDB using any PostgreSQL-compatible tool like pgAdmin.
From your favorite programming language, you can connect to Yugabyte just like you’d do for PostgreSQL.
SQL Actions
Load Sample Dataset
| |
Import data
| |
Run queries:
| |
Cleaning up
If you no longer want to run YugabyteDB, you can use these commands to clean up:
| |
If you used a bind mount (for example ~/yb_data) and want to delete persisted data too:
| |
If you used Docker Compose and want to remove containers and volumes:
| |
Conclusion
This should be enough information to get started using YugabyteDB locally in a Docker container or via Docker Compose.
Since YugabyteDB is PostgreSQL-compatible, it allows you to reuse lots of tools that you’re already familiar with.
You can easily migrate existing applications and workloads from PostgreSQL to YugabyteDB and benefit from its many features like auto-scaling capabilities.