Getting started with Managed Service for Sharded PostgreSQL
Note
The service is at the Preview stage.
With Managed Service for Sharded PostgreSQL, you can create and maintain sharded PostgreSQL (SPQR
To get started:
- Create a Sharded PostgreSQL cluster.
- Create a shard in the Sharded PostgreSQL cluster.
- Connect to the DB.
- Send your requests to the database.
Getting started
-
Navigate to the management console
and log in to Yandex Cloud or sign up if not signed up yet. -
If you do not have a folder yet, create one:
-
In the management console
, select the appropriate cloud from the list on the left. -
At the top right, click Create folder.
-
Give your folder a name. The naming requirements are as follows:
- It must be from 2 to 63 characters long.
- It can only contain lowercase Latin letters, numbers, and hyphens.
- It must start with a letter and cannot end with a hyphen.
-
Optionally, specify the description for your folder.
-
Select Create a default network. This will create a network with subnets in each availability zone. Within this network, you will also have a default security group, within which all network traffic will be allowed.
-
Click Create.
-
-
Assign the vpc.user and
managed-spqr.editorroles for the folder to your Yandex Cloud account to be able to create a cluster.Note
If you are unable to manage roles, contact your cloud or organization administrator.
-
You can connect to DB clusters from both inside and outside Yandex Cloud:
-
To connect from inside Yandex Cloud, create a Linux VM in the same network as the DB cluster.
-
To connect to the cluster from the internet, enable public access to hosts when creating the cluster.
Note
The next step implies connecting to the cluster from a VM. If your plan is to connect to the cluster from the internet, proceed to creating a cluster.
-
Create a cluster
-
In the management console
, select the folder where you want to create a Sharded PostgreSQL cluster. -
Select Yandex Managed Service for Sharded PostgreSQL.
-
Click Create cluster.
-
In the Cluster name field, enter a name for the cluster.
-
Select the
PRODUCTIONenvironment. -
Select the sharding type:
- Standard: Cluster will consist only of infrastructure hosts.
- Advanced: Cluster will consist only of router hosts and, optionally, coordinator hosts.
-
Under Network settings:
- Select the cloud network you created before you started out.
- Select the default security group or create a new one.
-
Specify the computing resource configuration:
- For standard sharding, specify the infrastructure host configuration under Infrastructure.
- For advanced sharding, specify the router host configuration under Router.
To specify your computing resource configuration:
-
Select the host class. Host class determines the technical specifications of the VMs the cluster hosts will be deployed on.
-
Under Storage:
-
Under Hosts, specify the availability zones and subnets for the hosts that will be created together with the cluster.
If you plan to connect to the cluster from the internet, enable Public access for the hosts.
-
Optionally, under Coordinator, enable Coordinator and specify the coordinator host configuration.
-
Under Database, specify:
- Database name. It must be unique within the folder.
- Database owner username.
- Password.
-
Under User, specify the password for the Sharded PostgreSQL console.
-
Click Create cluster.
-
Wait until the cluster is ready to work: its status will change to Running, and its state to Alive. To check its state, hover over the cluster status in the Availability column.
Create a shard in the cluster
-
In the management console
, select the folder where you created the Sharded PostgreSQL cluster. -
Create a Managed Service for PostgreSQL cluster in the same cloud network as the Sharded PostgreSQL cluster.
-
Open your Sharded PostgreSQL cluster and go to the
Shards tab. -
Click Create shard in the top-right corner of the page.
-
In the window that opens:
-
Specify the shard Name.
-
In the Managed Service for PostgreSQL cluster field, select the PostgreSQL cluster you previously created.
The Managed Service for PostgreSQL cluster must be in the same folder and cloud network as the Managed Service for Sharded PostgreSQL cluster.
-
Connect to the DB
Warning
If you are using security groups for a cloud network, configure them to enable all relevant traffic between the cluster and the connecting host.
To connect to the Sharded PostgreSQL cluster database:
-
Get an SSL certificate:
mkdir -p ~/.postgresql && \ wget "https://storage.yandexcloud.net/cloud-certs/CA.pem" \ --output-document ~/.postgresql/root.crt && \ chmod 0655 ~/.postgresql/root.crtThe certificate will be saved to the
~/.postgresql/root.crtfile. -
Install the required dependencies and the PostgreSQL client:
sudo apt update && sudo apt install -y postgresql-client -
Connect to the database:
psql "host=<host_FQDN> \ port=6432 \ sslmode=verify-full \ dbname=<DB_name> \ user=<username> \ target_session_attrs=read-write"Learn more about getting a host FQDN here.
-
Install the latest version of PostgreSQL for Windows
. Select the Command Line Tools install only. -
Get an SSL certificate:
mkdir $HOME\.postgresql; curl.exe -o $HOME\.postgresql\root.crt https://storage.yandexcloud.net/cloud-certs/CA.pemThe certificate will be saved to the
$HOME\.postgresql\root.crtfile. -
Set the environment variables for the connection:
$Env:PGSSLMODE="verify-full"; $Env:PGTARGETSESSIONATTRS="read-write" -
Connect to the database:
& "C:\Program Files\PostgreSQL\<PostgreSQL_major_version>\bin\psql.exe" ` --host=<host_FQDN>.mdb.yandexcloud.net ` --port=6432 ` --username=<username> ` <DB_name>Learn more about getting a host FQDN here.
Send your requests to the database
-
Create a table named
customersin the cluster database:CREATE TABLE IF NOT EXISTS customers ( name VARCHAR, phone VARCHAR, acctbal NUMERIC ); -
Populate the table with data:
INSERT INTO customers (name, phone, acctbal) VALUES ('John Doe', '123-45-67', 1500.50), ('Mary Johnson', '222-33-44', 3250.00), ('David Smith', '555-66-77', -50.75), ('Anna Davis', '111-22-33', 0.00), ('Paul Brown', '444-55-66', 780.30); -
Get the number of rows in the table:
SELECT COUNT(*) FROM customers;Result:
count ------- 5 (1 row)
What's next
- Read about the service concepts.
- Learn more about creating a cluster and connecting to a database.