[Yandex Cloud documentation](../index.md) > [Yandex Managed Service for PostgreSQL](index.md) > Getting started

# 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:


1. [Create a database cluster](#cluster-create).
1. [Connect to the database](#connect).
1. [Run database queries](#query-db).



## Getting started {#before-you-begin}

1. Navigate to the [management console](https://console.yandex.cloud) and log in to Yandex Cloud or sign up if not signed up yet.

1. If you do not have a folder yet, create one:

   1. In the [management console](https://console.yandex.cloud), in the top panel, click ![image](../_assets/console-icons/layout-side-content-left.svg) or ![image](../_assets/console-icons/chevron-down.svg) and select the [cloud](../resource-manager/concepts/resources-hierarchy.md#cloud).
   1. To the right of the cloud name, click ![image](../_assets/console-icons/ellipsis.svg).
   1. Select ![image](../_assets/console-icons/plus.svg) **Create folder**.
   
      ![create-folder1](../_assets/resource-manager/create-folder-1.png)
   
   1. Give your [folder](../resource-manager/concepts/resources-hierarchy.md#folder) a name. The naming requirements are as follows:
   
       * Length: between 3 and 63 characters.
       * It can only contain lowercase Latin letters, numbers, and hyphens.
       * It must start with a letter and cannot end with a hyphen.
   
   1. Optionally, specify the description for your folder.
   1. Select **Create a default network**. This will create a [network](../vpc/concepts/network.md#network) with subnets in each availability zone. Within this network, you will also have a [default security group](../vpc/concepts/security-groups.md#default-security-group), within which all network traffic will be allowed.
   1. Click **Create**.
   
      ![create-folder2](../_assets/resource-manager/create-folder-2.png)

1. [Assign](../iam/operations/roles/grant.md) the [vpc.user](../vpc/security/index.md#vpc-user) and [managed-postgresql.editor](security/index.md#managed-postgresql-editor) roles for the folder to your Yandex Cloud account. These roles enable you to create a cluster.

    {% note info %}
    
    If you cannot manage roles, contact your cloud or organization administrator.
    
    {% endnote %}

1. You can connect to DB clusters from both inside and outside Yandex Cloud:

   * To connect from inside Yandex Cloud, create a [Linux](../compute/quickstart/quick-create-linux.md) 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](#cluster-create).

   {% note info %}

   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](#cluster-create).

   {% endnote %}

1. [Connect](../compute/operations/vm-connect/ssh.md) to the VM over [SSH](https://en.wikipedia.org/wiki/Secure_Shell).


## Create a cluster {#cluster-create}

1. In the [management console](https://console.yandex.cloud), select the folder where you want to create your database cluster.
1. Navigate to **Managed Service for&nbsp;PostgreSQL**.
1. Click **Create cluster**.
1. In the **Cluster name** field, enter a name for the cluster.
1. Select the `PRODUCTION` environment.
1. Select the PostgreSQL version.
1. Select the [host class](concepts/instance-types.md). Host class determines the technical specifications of the [VMs](../compute/concepts/vm.md) the cluster [hosts](concepts/index.md) will be deployed on.
1. Under **Storage**:

    1. Select the [disk type](concepts/storage.md#storage-type-selection).
    1. Set the [storage](concepts/storage.md) size.

1. Under **Database**:

    1. Specify the database name. It must be unique within the folder.
    1. Specify the username of the DB owner.
    1. Enter your password or generate one using [Connection Manager](../metadata-hub/concepts/connection-manager.md).

1. Under **Network settings**:

    1. Select the [cloud network you created before you started out](#before-you-begin).
    1. Select the default security group or create a new one.

1. 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.

1. Click **Create cluster**.

1. 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 database {#connect}

{% note warning %}

If using security groups for the cloud network, [configure them](operations/connect/index.md#configuring-security-groups) to allow all required traffic between the cluster and your connection host.

{% endnote %}

To connect to the database:

{% list tabs group=operating_system %}

- Linux (Bash)/macOS (Zsh) {#linux-macos}

    1. Obtain an SSL certificate:

        ```bash
        mkdir -p ~/.postgresql && \
        wget "https://storage.yandexcloud.net/cloud-certs/CA.pem" \
                --output-document ~/.postgresql/root.crt && \
        chmod 0655 ~/.postgresql/root.crt
        ```

        The certificate will be saved to the `~/.postgresql/root.crt` file.

    1. Install the required dependencies and the PostgreSQL client:

        ```bash
        sudo apt update && sudo apt install -y postgresql-client
        ```

    1. Connect to the database:

        ```bash
        psql "host=<list_of_cluster_hosts> \
              port=6432 \
              sslmode=verify-full \
              dbname=<DB_name> \
              user=<username> \
              target_session_attrs=read-write"
        ```

        You can get the cluster ID with the [list of clusters in the folder](operations/cluster-list.md#list-clusters).

- Windows (PowerShell) {#windows}

    1. Install the same [PostgreSQL for Windows](https://www.postgresql.org/download/windows/) version that is used in the cluster. Install only the _Command Line Tools_.

    1. Obtain an SSL certificate:

        ```powershell
        mkdir $HOME\.postgresql; curl.exe -o $HOME\.postgresql\root.crt https://storage.yandexcloud.net/cloud-certs/CA.pem
        ```

        The certificate will be saved to the `$HOME\.postgresql\root.crt` file.

    1. Set the environment variables for the connection:

        ```powershell
        $Env:PGSSLMODE="verify-full"; $Env:PGTARGETSESSIONATTRS="read-write"
        ```

    1. Connect to the database:

        ```powershell
        & "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](operations/cluster-list.md#list-clusters).

{% endlist %}

## Send your requests to the database {#query-db}

1. Create a table named `customers` in the cluster database:

    ```sql
    CREATE TABLE IF NOT EXISTS customers (
        name VARCHAR,
        phone VARCHAR,
        acctbal NUMERIC
    );
    ```

1. Populate the table with data:

    ```sql
    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);
    ```

1. Get the number of rows in the table:

    ```sql
    SELECT COUNT(*) FROM customers;
    ```

    Result:

    ```sql
    count
    -------
        5
    (1 row)
    ```


## What's next {#whats-next}

* [Transfer data](../data-transfer/tutorials/managed-postgresql.md#quick-transfer) to the cluster database.
* Read about the [service concepts](concepts/index.md).
* Learn more about [creating a cluster](operations/cluster-create.md) and [connecting to a database](operations/connect/index.md).
* Check out our user [tutorials](tutorials/index.md).
* Check out the [questions and answers](qa/general.md).