Yandex.Cloud
  • Services
  • Why Yandex.Cloud
  • Pricing
  • Documentation
  • Contact us
Get started
Yandex Managed Service for PostgreSQL
  • Getting started
  • Step-by-step instructions
    • All instructions
    • Information about existing clusters
    • Creating clusters
    • Connecting to databases
    • Stop and start the cluster
    • SQL queries in the management console
    • Changing cluster and database settings
    • Connecting to DataLens
    • Managing PostgreSQL hosts
    • Database management
    • How to manage PostgreSQL extensions
    • Managing database users
    • Granting permissions and roles
    • Managing backups
    • Deleting clusters
    • Database migration to the Cloud
    • Creating a logical replica of Amazon RDS for PostgreSQL in Managed Service for PostgreSQL
  • Solutions
    • Creating a PostgreSQL cluster for 1C
  • Concepts
    • Relationship between service resources
    • Host classes
    • Network in Yandex Managed Service for PostgreSQL
    • Quotas and limits
    • Storage types
    • Backups
    • Assigning roles
    • Replication
    • Supported clients
  • Access management
  • Pricing policy
    • Current pricing policy
    • Archive
      • Before January 1, 2019
      • From January 1 to March 1, 2019
      • From March 1, 2019 to February 1, 2020
  • API reference
    • Authentication in the API
    • gRPC
      • Overview
      • BackupService
      • ClusterService
      • DatabaseService
      • ResourcePresetService
      • UserService
      • OperationService
    • REST
      • Overview
      • Backup
        • Overview
        • get
        • list
      • Cluster
        • Overview
        • addHosts
        • backup
        • create
        • delete
        • deleteHosts
        • get
        • list
        • listBackups
        • listHosts
        • listLogs
        • listOperations
        • move
        • rescheduleMaintenance
        • restore
        • start
        • startFailover
        • stop
        • streamLogs
        • update
        • updateHosts
      • Database
        • Overview
        • create
        • delete
        • get
        • list
        • update
      • ResourcePreset
        • Overview
        • get
        • list
      • User
        • Overview
        • create
        • delete
        • get
        • grantPermission
        • list
        • revokePermission
        • update
      • Operation
        • Overview
        • get
  • Questions and answers
    • General questions
    • Questions about PostgreSQL
    • All questions on the same page
  1. Step-by-step instructions
  2. Connecting to databases

Connecting to a database in a cluster PostgreSQL

  • Configuring an SSL certificate
  • Connection string
  • Automatic master host selection
    • Using libpq
    • With a driver that supports only one host

You can connect to Managed Service for PostgreSQL cluster hosts:

  • Over the internet, if you configured public access for the appropriate host. You can only connect to these hosts over an SSL connection.
  • From Yandex.Cloud VM instances hosted in the same virtual network. If the host isn't publicly available, you don't need to use an SSL connection to connect to such VMs.

Note

If public access is only configured for certain hosts in your cluster, automatic master change may make the master unavailable over the internet.

Configuring an SSL certificate

PostgreSQL hosts with public access only support connections with an SSL certificate. You can prepare a certificate as follows:

$ mkdir ~/.postgresql
$ wget "https://storage.yandexcloud.net/cloud-certs/CA.pem" -O ~/.postgresql/root.crt
$ chmod 0600 ~/.postgresql/root.crt

Connection string

Connect to the database using the command psql.

To view an example of the command with the host FQDN filled in, open the cluster page in the management console and click Connect.

SSL
Without SSL

For hosts with public access, you can only connect over the internet with an SSL certificate.

$ psql "host=<DB host FQDN> \
        port=6432 \
        sslmode=verify-full \
        dbname=<DB name> \
        user=<DB user name>"

If you don't need to encrypt traffic within the virtual network when connecting to the database, you can connect to the database without an SSL connection. Pass the sslmode parameter with the disable value:

$ psql "host=<DB host FQDN> \
        port=6432 \
        sslmode=disable \
        dbname=<DB name> \
        user=<DB user name>"

Automatic master host selection

Using libpq

To guarantee a connection to the master host, specify the FQDNs of all the cluster hosts in the host argument and pass the target_session_attrs=read-write parameter. This parameter is supported by the libpq library starting from version 10:

psql "host=<host 1 FQDN>,<host 2 FQDN>,<host 3 FQDN> \
      port=6432 \
      sslmode=verify-full \
      dbname=<DB name> \
      user= \
      target_session_attrs=read-write"

To upgrade the library version used by the psql utility:

  • For Debian-based Linux distributions, install the postgresql-client-10 package (for example, using an APT repository).

  • For operating systems that use RPM packages, a PostgreSQL distribution is available from a YUM repository.

You can find the addresses of all the hosts in the DB cluster on the appropriate cluster page in the management console.

With a driver that supports only one host

If your database connection driver doesn't support passing multiple hosts in the connection string , you can connect to a special host like c-<cluster ID>.rw.mdb.yandexcloud.net.

This domain name always indicates the current master in the cluster. For example, you can connect to the master of the cluster with the c9qash3nb1v9ulc8j9nm ID as follows:

$ psql "host=c-c9qash3nb1v9ulc8j9nm.rw.mdb.yandexcloud.net \
      port=6432 \
      sslmode=verify-full \
      dbname=<DB name> \
      user=<DB user name>"
In this article:
  • Configuring an SSL certificate
  • Connection string
  • Automatic master host selection
  • Using libpq
  • With a driver that supports only one host
Language
Careers
Privacy policy
Terms of use
© 2021 Yandex.Cloud LLC