A PostgreSQL-Compatible, Distributed SQL Cheat Sheet: The Basics
In this article, take a look at the basics of a PostgreSQL-compatible, distributed SQL cheat sheet.
Join the DZone community and get the full member experience.Join For Free
In this blog post, we’ll highlight how all the basic commands you end up using in the first few minutes after installing PostgreSQL are identical in YugabyteDB. We’ll cover connecting to the database, creating users, databases, schemas, and calling external files from the SQL shell. In the next blog post in this series we’ll tackle querying data to demonstrate that if you know how to query data in PostgreSQL, you already know how to do it in YugabyteDB.
First things first, for those of you who might be new to either distributed SQL or YugabyteDB…
Distributed SQL databases are becoming popular with organizations interested in moving data infrastructure to the cloud or cloud-native environments. This is often motivated by the desire to reduce TCO or move away from the horizontal scaling limitations of monolithic RDBMS like Oracle, PostgreSQL, MySQL, and SQL Server. The basic characteristics of Distributed SQL are:
- They must have a SQL API for querying and modeling data, with support for traditional RDBMS features like foreign keys, partial indexes, stored procedures and triggers.
- Smart distributed query execution so that query processing is pushed closer to the data as opposed to data being pushed over the network and thus slowing down query response times.
- Should support automatic and transparent distributed data storage. This includes indexes that should be sharded across multiple nodes of the cluster so that no single node becomes a bottleneck. Data distribution ensures high performance and high availability.
- Distributed SQL systems should also provide for strongly consistent replication and distributed ACID transactions.
For a deeper discussion about what Distributed SQL is, check out, “What is Distributed SQL?”
YugabyteDB is an open source, high-performance distributed SQL database built on a scalable and fault-tolerant design inspired by Google Spanner. YugabyteDB is PostgreSQL wire compatible, cloud native, offers deep integration with GraphQL projects, plus supports advanced RDBMS features like stored procedures, triggers, and UDFs.
Got questions? Make sure to ask them in our YugabyteDB Slack channel. Ok, let’s dive in…
YugabyteDB is only slightly more involved than getting PostgreSQL up and running. At the end of the day it should only take a few minutes or less depending on your environment. Let’s look at a few scenarios:
Note: If you want to run 3 local nodes instead of a single node for either the Mac or Linux setups, just tweak the last command so it reads:
For more information on other installation types and prerequisites, check out the Quickstart Docs.
Assuming you are in the YugabyteDB install directory, simply execute the following to get to a YSQL shell:
Assuming you are connected to the Kubernetes cluster via the Google Cloud Console, execute the following.
Check out the documentation for more information about YugabyteDB’s PostgreSQL-compatible YSQL API.
Assuming we are using the PostgreSQL JDBC driver to connect to YugabyteDB, the construction of the connect string will be identical to PostgreSQL. For example here’s a snippet for setting up a connection to a database called "northwind" in YugabyteDB using the PostgreSQL driver in Spring.
Note: In the example above we assume YugabyteDB’s YSQL API is being accessed at 126.96.36.199 on the default port 5433, using the default user “yugabyte” with the password “password”. For more information about YugabyteDB connectivity options check out the Drivers section of the documentation.
Creating roles/users, and assigning them privileges and passwords is going to be the same in YugabyteDB as it is in PostgreSQL.
For more information about how YugabyteDB handles users, permissions, security, and encryption check out the Secure section of the documentation.
Creating databases and schemas in YugabyteDB is identical to how it is done in PostgreSQL.
If you have DDL or DML scripts that you want to call from within the YSQL shell, the process is the same in YugabyteDB as it is in PostgreSQL. You can find the scripts used in the examples below in the "~/yugabyte-2.3.x.x/share" directory. For information about the sample data sets that ship by default with YugabyteDB, check out the Sample Datasets documentation.
Stay tuned for part 2 in this series where we’ll dive into querying data from a YugabyteDB cluster using familiar PostgreSQL syntax.
Opinions expressed by DZone contributors are their own.