[Yandex Cloud documentation](../../../index.md) > [Yandex Managed Service for PostgreSQL](../../index.md) > [Step-by-step guides](../index.md) > [Connection](index.md) > FQDNs of hosts

# FQDNs of PostgreSQL hosts

To connect to a host, you need its fully qualified domain name ([FQDN](../../concepts/network.md#hostname)). You can get it using one of the following methods:

* [Request a list of cluster hosts](../hosts.md#list-hosts).
* In the [management console](https://console.yandex.cloud), copy the cluster connection command (it contains the host’s FQDN). To get the command, go to the cluster page and click **Connect**.
* Look up the FQDN in the management console:

   1. Navigate to the cluster page.
   1. Navigate to **Hosts**.
   1. Copy the **Host FQDN** column value.

Cluster hosts also use [special FQDNs](#special-fqdns).

## Special FQDNs {#special-fqdns}

Alongside [regular FQDNs](#), Managed Service for PostgreSQL offers special FQDNs that can be also used for cluster connections.

In multiple-host clusters, special FQDNs may for some time (up to 10 minutes) point to the old host, even if it has changed its role (e.g., from a master to a replica). If using a special FQDN which points to the current master, some write requests may fail if routed to the replica. This is because it takes time to update DNS records for special FQDNs. If your write request returns an error, repeat it later.

### Current master {#fqdn-master}

An FQDN in `c-<cluster_ID>.rw.mdb.yandexcloud.net` format always points to the current master host in the cluster. You can get the cluster ID from the [folder’s cluster list](../cluster-list.md#list-clusters).

When connecting to this FQDN, you can perform read and write operations.

{% note warning %}

Use master host special FQDN-based connections only for processes that can cope with database being unavailable for writing for up to 10 minutes.

{% endnote %}

### Most recent replica {#fqdn-replica}

An FQDN in `c-<cluster_ID>.ro.mdb.yandexcloud.net` format points to the [replica](../../concepts/replication.md) that is most up-to-date with the current master. You can get the cluster ID with the [list of clusters in the folder](../cluster-list.md#list-clusters).

**Specifics:**

* When connecting to this FQDN, you can only perform read operations.
* If there are no active replicas in the cluster, this FQDN will point to the current master host.
* Replicas with a [manually configured replication source](../../concepts/replication.md#replication-manual) cannot be selected as most up-to-date replicas when using this FQDN.

## Selecting an FQDN and cluster connection method {#automatic-master-host-selection}

You can connect to a cluster using its [host FQDNs](#) or [special FQDNs](#special-fqdns): If the cluster [consists of several hosts](../../concepts/planning-cluster-topology.md), keep in mind that the current master can become a replica at any moment, and vice versa.

{% note warning %}

If automatic failover promotes a host without public access to master or most up-to-date replica, that host will be unreachable from the internet. To avoid this, [enable public access](../hosts.md#update) for all cluster hosts.

{% endnote %}

Use one of the following methods to connect to the master host with read and write access:

* Connect using the [special FQDN](#fqdn-master) pointing to the current master.

    After a master failover, this FQDN may temporarily point to the previous master, now a replica, due to the time required for the DNS record to update.

    Therefore, applications using this FQDN must be designed to handle temporary master unavailability. For example, they should retry write requests after a short delay.

    {% cut "Connection example" %}

    In this example, we use the `c9qash3nb1v9********` cluster ID:

    ```bash
    psql "host=c-c9qash3nb1v9********.rw.mdb.yandexcloud.net \
          port=6432 \
          sslmode=verify-full \
          dbname=<DB_name> \
          user=<username>"
    ```

    {% endcut %}

* Connect by listing all cluster hosts and specifying `target_session_attrs=read-write`.

    {% cut "Connection example" %}

    Here, we list all the cluster’s hosts.

    The host IDs are `rc1a-be***.mdb.yandexcloud.net`, `rc1b-5r***.mdb.yandexcloud.net`, and `rc1d-t4***.mdb.yandexcloud.net`:

    ```bash
    psql "host=rc1a-be***.mdb.yandexcloud.net,rc1b-5r***.mdb.yandexcloud.net,rc1d-t4***.mdb.yandexcloud.net \
          port=6432 \
          sslmode=verify-full \
          dbname=<DB_name> \
          user=<username> \
          target_session_attrs=read-write"
    ```

    {% endcut %}

Use one of the following methods to connect to the host with read access:

* Connect using a [special FQDN](#fqdn-replica) pointing to the most up-to-date replica.

    {% cut "Connection example" %}

    In this example, we use the `c9qash3nb1v9********` cluster ID:

    ```bash
    psql "host=c-c9qash3nb1v9********.ro.mdb.yandexcloud.net \
          port=6432 \
          sslmode=verify-full \
          dbname=<DB_name> \
          user=<username>"
    ```

    {% endcut %}

* Connect by listing all cluster hosts and specifying `target_session_attrs=any`.

    {% cut "Connection example" %}

    Here, we list all the cluster’s hosts.

    The host IDs are `rc1a-be***.mdb.yandexcloud.net`, `rc1b-5r***.mdb.yandexcloud.net`, and `rc1d-t4***.mdb.yandexcloud.net`:

    ```bash
    psql "host=rc1a-be***.mdb.yandexcloud.net,rc1b-5r***.mdb.yandexcloud.net,rc1d-t4***.mdb.yandexcloud.net \
          port=6432 \
          sslmode=verify-full \
          dbname=<DB_name> \
          user=<username> \
          target_session_attrs=any"
    ```

    {% endcut %}

{% note info %}

You can use the `target_session_attrs` setting when connecting via a client utilizing the `libpq` library.

Support for the `read-write` value in this setting was introduced in `libpq` [version 10](https://www.postgresql.org/docs/10/static/libpq-connect.html).

{% cut "How to update the library version used by `psql`" %}

* For Debian-based Linux distributions, install the `postgresql-client-10` package or a newer version from the [official APT repository](https://www.postgresql.org/download/linux/ubuntu/) (or another source of your choice).
* For RPM-based operating systems, use the PostgreSQL distribution from the [yum repository](https://yum.postgresql.org/).

{% endcut %}

{% endnote %}