Connect to a PostgreSQL cluster
You can connect to a PostgreSQL cloud database cluster by DNS address, private IP address, and public IP address.
We recommend connecting by DNS address because the DNS addresses correspond to the node roles and lead to the actual IP addresses of the master and replicas. IP addresses correspond to specific nodes. If the master is unavailable, one of the replicas will assume its role, the master's IP address will change, and the IP connection will stop working.
If the cluster is connected to a private subnet and you want to work with it via DNS, connect cloud router-to-external-network to the cluster subnet.
If the cluster is connected to a private network and you need to configure access to the node from the Internet, connect public IP address.
Ports
Use ports to connect to PostgreSQL:
- 5433 — port to connect to the selected node via connection-pooler — allows to reduce the load on PostgreSQL;
- 5432 is the port to connect directly to the PostgreSQL process.
Ways to connect
- through the psql terminal client;
- through graphical database management tools: pgAdmin or an office suite with ODBC or JDBC support;
- from the with SSL and without SSL program code.
Connection with SSL certificate is available for all methods.
View the address to connect
- In Control Panel, go to Cloud Platform → Databases.
- Open the Database Cluster page → Connect tab.
- In the Addresses to connect block, look up the address.
Connect with SSL
Connecting using TLS/SSL encryption provides a secure connection between your server and the database cluster.
- Bash
- PowerShell
- Python
- PHP
- Go
- Node.js
-
Download the root certificate and place it in the
~/.postgresql/
folder:mkdir -p ~/.postgresql/
wget https://storage.dbaas.selcloud.ru/CA.pem -O ~/.postgresql/root.crt
chmod 0600 ~/.postgresql/root.crt -
Connect to the cluster:
psql --host=<host> \
--port=<port> \
--user=<database_user_name> \
--dbname=<database_name> \
--sslmode=verify-caSpecify:
<host>
— DNS address of the node;<port>
is the port to connect to;<database_user_name>
is the database username;<database_name>
is the name of the database.
-
In Control Panel, click Download Certificate to download the root certificate and place it in the
%APPDATA%\postgresql\
folder. -
Connect to the cluster:
psql --host=<host> \
--port=<port> \
--user=<database_user_name> \
--dbname=<database_name> \
--sslmode=verify-caSpecify:
<host>
— DNS address of the node;<port>
is the port to connect to;<database_user_name>
is the database username;<database_name>
is the name of the database.
-
Download the root certificate and place it in the
~/.postgresql/
folder:mkdir -p ~/.postgresql/
wget https://storage.dbaas.selcloud.ru/CA.pem -O ~/.postgresql/root.crt
chmod 0600 ~/.postgresql/root.crt -
Install the psycopg2 library:
pip3 install psycopg2-binary
-
Use the connection example:
import psycopg2
conn = psycopg2.connect("""
host=<host>
dbname=<database_name>
user=<database_user_name>
password=<password>
sslrootcert=<full_path_to_root_certificate>
sslmode=verify-ca
port=<port>
""")
cur = conn.cursor()
cur.execute('SELECT 40+2')
print(cur.fetchone())
cur.close()
conn.close()Specify:
<host>
— DNS address of the node;<database_name>
is the name of the database;<database_user_name>
is the database username;<password>
is the user's password;<full_path_to_root_certificate>
is the full path to the root certificate;<port>
is the port to connect to.
-
Download the root certificate and place it in the
~/.postgresql/
folder:mkdir -p ~/.postgresql/
wget https://storage.dbaas.selcloud.ru/CA.pem -O ~/.postgresql/root.crt
chmod 0600 ~/.postgresql/root.crt -
Install the pgsql library:
apt install php-pgsql
-
Use the connection example:
<?php
$dbconn = pg_connect("
host=<host>
dbname=<database_name>
user=<database_user_name>
password=<password>
sslrootcert=<full_path_to_root_certificate>
sslmode=verify-ca
port=<port>
");
$query = 'SELECT 40 + 2';
$result = pg_query($query);
$row = pg_fetch_row($result);
echo $row[0];
pg_close($dbconn);
?>Specify:
<host>
— DNS address of the node;<database_name>
is the name of the database;<database_user_name>
is the database username;<password>
is the user's password;<full_path_to_root_certificate>
is the full path to the root certificate;<port>
is the port to connect to.
-
Download the root certificate and place it in the
~/.postgresql/
folder:mkdir -p ~/.postgresql/
wget https://storage.dbaas.selcloud.ru/CA.pem -O ~/.postgresql/root.crt
chmod 0600 ~/.postgresql/root.crt -
Use the connection example:
package main
import (
"context"
"fmt"
"os"
"github.com/jackc/pgx/v4"
)
func main() {
connectionString := fmt.Sprintf("postgres://%s:%s@%s:5432/%s?sslmode=verify-ca&sslrootcert=%s",
"<database_user_name>",
"<password>",
"<host>",
"<database_name>",
"<full_path_to_root_certificate>"
)
conn, err := pgx.Connect(context.Background(), connectionString)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
os.Exit(1)
}
defer conn.Close(context.Background())
var sum int64
err = conn.QueryRow(context.Background(), "SELECT 40+2").Scan(&sum)
if err != nil {
fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
os.Exit(1)
}
fmt.Println(sum)
}Specify:
<database_user_name>
is the database username;<password>
is the user's password;<host>
— DNS address of the node;<database_name>
is the name of the database;<full_path_to_root_certificate>
is the full path to the root certificate.
-
Download the root certificate and place it in the
~/.postgresql/
folder:mkdir -p ~/.postgresql/
wget https://storage.dbaas.selcloud.ru/CA.pem -O ~/.postgresql/root.crt
chmod 0600 ~/.postgresql/root.crt -
Install the pg library:
npm install pg
-
Use the connection example:
const fs = require('fs');
const pg = require('pg');
const config = {
host: '<host>',
port: '<port>',
database: '<database_name>',
user: '<database_user_name>',
password: '<password>',
ssl: {
rejectUnauthorized: true,
ca: fs.readFileSync('<full_path_to_root_certificate>').toString(),
},
};
const client = new pg.Client(config);
client.connect((error) => {
if (error) throw error;
});
client.query('SELECT 40 + 2 AS sum', (error, res) => {
if (error) throw error;
console.log(res.rows);
client.end();
});Specify:
<host>
— DNS address of the node;<port>
is the port to connect to;<database_name>
is the name of the database;<database_user_name>
is the database username;<password>
is the user's password;<full_path_to_root_certificate>
is the full path to the root certificate.
Connect without SSL
- Bash
- PowerShell
- Python
- PHP
- Go
- Node.js
-
Open the CLI.
-
Connect to the cluster:
psql --host=<host> {
--port=<port> {
--user=<database_user_name> {
--dbname=<database_name> {
--sslmode=disableSpecify:
<host>
— DNS address of the node;<port>
is the port to connect to;<database_user_name>
is the database username;<database_name>
is the name of the database.
-
Open the CLI.
-
Connect to the cluster:
psql --host=<host> {
--port=<port> {
--user=<database_user_name> {
--dbname=<database_name> {
--sslmode=disableSpecify:
<host>
— DNS address of the node;<port>
is the port to connect to;<database_user_name>
is the database username;<database_name>
is the name of the database.
-
Install the psycopg2 library:
pip3 install psycopg2-binary
-
Use the connection example:
import psycopg2
conn = psycopg2.connect("""
host=<host>
dbname=<database_name>
user=<database_user_name>
password=<password>
port=<port>
""")
cur = conn.cursor()
cur.execute('SELECT 40+2')
print(cur.fetchone())
cur.close()
conn.close()Specify:
<host>
— DNS address of the node;<database_name>
is the name of the database;<database_user_name>
is the database username;<password>
is the user's password;<port>
is the port to connect to.
-
Install the pgsql library:
apt install php-pgsql
-
Use the connection example:
<?php
$dbconn = pg_connect("
host=<host>
dbname=<database_name>
user=<database_user_name>
password=<password>
port=<port>
");
$query = 'SELECT 40 + 2';
$result = pg_query($query);
$row = pg_fetch_row($result);
echo $row[0];
pg_close($dbconn);
?>Specify:
<host>
— DNS address of the node;<database_name>
is the name of the database;<database_user_name>
is the database username;<password>
is the user's password;<port>
is the port to connect to.
Use the connection example:
package main
import (
"context"
"fmt"
"os"
"github.com/jackc/pgx/v4"
)
func main() {
connectionString := fmt.Sprintf("postgres://%s:%s@%s:5432/%s",
"<database_user_name>",
"<password>",
"<host>",
"<database_name>",
)
conn, err := pgx.Connect(context.Background(), connectionString)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
os.Exit(1)
}
defer conn.Close(context.Background())
var sum int64
err = conn.QueryRow(context.Background(), "SELECT 40+2").Scan(&sum)
if err != nil {
fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
os.Exit(1)
}
fmt.Println(sum)
}
Specify:
<database_user_name>
is the database username;<password>
is the user's password;<host>
— DNS address of the node;<database_name>
is the name of the database.
-
Install the pg library:
npm install pg
-
Use the connection example:
const pg = require('pg');
const config = {
host: '<host>',
port: '<port>',
database: '<database_name>',
user: '<database_user_name>',
password: '<password>',
};
const client = new pg.Client(config);
client.connect((error) => {
if (error) throw error;
});
client.query('SELECT 40 + 2 AS sum', (error, res) => {
if (error) throw error;
console.log(res.rows);
client.end();
});Specify:
<host>
— DNS address of the node;<port>
is the port to connect to;<database_name>
is the name of the database;<database_user_name>
is the database username;<password>
is the user's password.