Getting started with Managed Service for PostgreSQL
Managed Service for PostgreSQL helps you create and maintain PostgreSQL clusters in the Yandex Cloud infrastructure.
To get started:
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-postgresql.editor roles 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, request 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 DB cluster. -
Select Managed Service for PostgreSQL.
-
Click Create cluster.
-
In the Cluster name field, enter a name for the cluster.
-
Select the
PRODUCTIONenvironment. -
Select the PostgreSQL version.
-
Select the host class. Host class determines the technical specifications of the VMs the cluster hosts will be deployed on.
-
Under Storage:
-
Under Database:
- Specify the DB name. It must be unique within the folder.
- Specify the user name of the DB owner.
- Enter your password or generate one using Connection Manager.
-
Under Network settings:
- Select the cloud network you created before you started out.
- Select the default security group or create a new one.
-
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.
-
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.
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 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=c-<cluster_ID>.rw.mdb.yandexcloud.net \ port=6432 \ sslmode=verify-full \ dbname=<DB_name> \ user=<user_name> \ target_session_attrs=read-write"You can get the cluster ID with the list of clusters in the folder.
-
Install the same PostgreSQL for Windows
version that is used in the cluster. 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=c-<cluster_ID>.rw.mdb.yandexcloud.net ` --port=6432 ` --username=<username> ` <DB_name>You can get the cluster ID with the list of clusters in the folder.
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
- Transfer data to the cluster database.
- Read about the service concepts.
- Learn more about creating a cluster and connecting to a database.
- Check out our user tutorials.
- Check questions and answers.