> ## Documentation Index
> Fetch the complete documentation index at: https://docs.nebius.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Connecting to databases in Managed Service for PostgreSQL®

To work with a database in a Managed Service for PostgreSQL cluster, you need to connect to it.

## Prerequisites

1. [Create a cluster](https://docs.nebius.com/postgresql/clusters/manage.md#how-to-create-clusters) and securely store the password you specified during creation.

2. To connect to a database, install `psql`, a PostgreSQL command line client:

   > [tab] macOS
   >
   > ```bash theme={null}
   > brew install libpq
   > echo 'export PATH="/opt/homebrew/opt/libpq/bin:$PATH"' >> ~/.zshrc
   > ```
   >
   > After that, restart the terminal.

   > [tab] Ubuntu
   >
   > ```bash theme={null}
   > sudo apt update && sudo apt install postgresql-client
   > ```

3. To secure connections to databases, install the Nebius AI Cloud certificate:
   ```bash theme={null}
   mkdir -p ~/.postgresql \
   curl "https://storage.eu-north1.nebius.cloud/msp-certs/ca.pem" \
      -o ~/.postgresql/root.crt \
   chmod 0600 ~/.postgresql/root.crt
   ```
   This command creates the `.postgresql` folder, downloads the certificate into it and then sets the access rights to the certificate file.

4. Prepare the interface that you use to get connection endpoints.

   If you use the web console, you can skip this step.

   If you use the CLI or an SDK, install and configure one of the following tools:

   > [tab] CLI
   >
   > [Install and configure](https://docs.nebius.com/cli/install.md) the Nebius AI Cloud CLI.

   > [tab] Go SDK
   >
   > [Install and initialize the Nebius SDK for Go](https://docs.nebius.com/sdk/go/install-auth.md).

   > [tab] Python SDK
   >
   > [Install and initialize the Nebius SDK for Python](https://docs.nebius.com/sdk/python/install-auth.md).

   > [tab] JavaScript SDK
   >
   > [Install and initialize the Nebius SDK for JavaScript](https://docs.nebius.com/sdk/javascript/install-auth.md).

## How to connect to a database

Go to the web console and get the connection command with all data filled in for your cluster:

> [tab] Web console
>
> 1. In the sidebar, go to  **PostgreSQL**.
> 2. Click your cluster.
> 3. Click **How to connect**.
> 4. Copy the connection command from the window that opens.

Enter this command in the terminal. You will be asked for the password you specified when [creating the cluster](https://docs.nebius.com/postgresql/clusters/manage.md#how-to-create-clusters).

### Details of the connection command

The connection command that you copy from the web console looks like this:

```bash theme={null}
psql "host=<cluster_endpoint> \
  port=5432 \
  sslmode=verify-full \
  dbname=<DB_name> \
  user=<username>"
```

It contains the following parameters:

* `<DB_name>`: Default database name that you specified when creating the cluster. If you created another database in this cluster, you can use its name instead.

* `<username>`: Username you specified when creating the cluster.

  > [!NOTE]
  >
  > Once you create other databases and [users](https://docs.nebius.com/postgresql/databases/users.md#create-a-new-user), you can use their names instead.

* `<cluster_endpoint>`: Depends on where you are connecting from:

  * To connect from the internet, use the public cluster endpoint. You can connect from the internet only if you enabled public access to the cluster when you created it.
  * To connect from other Nebius AI Cloud resources, use the private cluster endpoint.

To get the endpoints:

> [tab] Web console
>
> 1. In the sidebar, go to  **PostgreSQL**.
> 2. Click your cluster.
> 3. Click **Copy endpoint URL** and select **Public RW endpoint URL** to copy the public cluster endpoint, or **Private RW endpoint URL** to copy the private cluster endpoint.

> [tab] CLI
>
> Get the URLs from cluster data:
>
> ```bash theme={null}
> nebius msp postgresql v1alpha1 cluster get-by-name \
>   --name <cluster_name> \
>   --format jsonpath='{.status.connection_endpoints.public_read_write}'
>
> nebius msp postgresql v1alpha1 cluster get-by-name \
>   --name <cluster_name> \
>   --format jsonpath='{.status.connection_endpoints.private_read_write}'
> ```

> [tab] Go SDK
>
> Get the URLs from cluster data:
>
> ```go theme={null}
> endpointService := sdk.Services().MSP().PostgreSQL().
>     V1Alpha1().Cluster()
> publicCluster, err := endpointService.GetByName(
>     ctx,
>     &common.GetByNameRequest{
>         ParentId: "<project_ID>",
>         Name: "<cluster_name>",
>     },
> )
> if err != nil {
>     return fmt.Errorf("get by name: %w", err)
> }
> fmt.Println(publicCluster.GetStatus().
>     GetConnectionEndpoints().GetPublicReadWrite())
> privateCluster, err := endpointService.GetByName(
>     ctx,
>     &common.GetByNameRequest{
>         ParentId: "<project_ID>",
>         Name: "<cluster_name>",
>     },
> )
> if err != nil {
>     return fmt.Errorf("get by name: %w", err)
> }
> fmt.Println(privateCluster.GetStatus().
>     GetConnectionEndpoints().GetPrivateReadWrite())
> ```
>
> In the code, update the following values:
>
> * `GetByNameRequest.ParentId`: Project ID.
> * `GetByNameRequest.Name`: Cluster name.
>
> The response returns the endpoints in:
>
> * `Status.ConnectionEndpoints.PublicReadWrite`
> * `Status.ConnectionEndpoints.PrivateReadWrite`

> [tab] Python SDK
>
> Get the URLs from cluster data:
>
> ```python theme={null}
> endpoint_service = pg.ClusterServiceClient(sdk)
> public_cluster = await endpoint_service.get_by_name(
>     GetByNameRequest(
>         parent_id="<project_ID>",
>         name="<cluster_name>",
>     ),
> )
> print(public_cluster.status.connection_endpoints.public_read_write)
> private_cluster = await endpoint_service.get_by_name(
>     GetByNameRequest(
>         parent_id="<project_ID>",
>         name="<cluster_name>",
>     ),
> )
> print(private_cluster.status.connection_endpoints.private_read_write)
> ```
>
> In the code, update the following values:
>
> * `GetByNameRequest.parent_id`: Project ID.
> * `GetByNameRequest.name`: Cluster name.
>
> The response returns the endpoints in:
>
> * `status.connection_endpoints.public_read_write`
> * `status.connection_endpoints.private_read_write`

> [tab] JavaScript SDK
>
> Get the URLs from cluster data:
>
> ```ts theme={null}
> const endpointService = new pg.ClusterService(sdk);
> const publicCluster = await endpointService.getByName(
>   GetByNameRequest.create({
>     parentId: "<project_ID>",
>     name: "<cluster_name>",
>   }),
> ).result;
> console.log(
>   publicCluster.status?.connectionEndpoints?.publicReadWrite ?? "",
> );
> const privateCluster = await endpointService.getByName(
>   GetByNameRequest.create({
>     parentId: "<project_ID>",
>     name: "<cluster_name>",
>   }),
> ).result;
> console.log(
>   privateCluster.status?.connectionEndpoints?.privateReadWrite ?? "",
> );
> ```
>
> In the code, update the following values:
>
> * `GetByNameRequest.parentId`: Project ID.
> * `GetByNameRequest.name`: Cluster name.
>
> The response returns the endpoints in:
>
> * `status.connectionEndpoints.publicReadWrite`
> * `status.connectionEndpoints.privateReadWrite`

***

*Postgres, PostgreSQL and the Slonik Logo are trademarks or registered trademarks of the PostgreSQL Community Association of Canada, and used with their permission.*
