Connect to a cluster by using DMS, pgAdmin, psql, or a programming language.
Prerequisites
Before you connect:
You can now connect to the cluster.
Connect to a cluster
Choose a connection method.
Use DMS to connect to a cluster
Data Management (DMS) provides database administration, security auditing, and performance optimization. Manage your PolarDB cluster directly in DMS.
-
Log on to the PolarDB console. In the cluster list, click a cluster ID to open the Basic Information page. In the upper-right corner, click Log On To Database.
-
In the dialog box, enter the database account and password, then click Login.
-
After you log on, choose in the left-side navigation pane to manage the PolarDB cluster.

Use a client to connect to a cluster
This procedure uses pgAdmin 4 v9.0 to connect to a PolarDB cluster.
-
Download and install the pgAdmin 4 client.
-
Open the pgAdmin 4 client, right-click Servers, and select .

-
On the General tab, set the connection name. On the Connection tab, configure the connection parameters, then click Save.

Parameter
Description
Host name/address
The endpoint and port of the PolarDB cluster.
-
To access the PolarDB cluster from an ECS instance, and the ECS instance is in the same VPC as the PolarDB cluster, specify the Private endpoint and port.
-
To access the PolarDB cluster from your on-premises environment, specify the Public endpoint and port.
-
The default port number is .
Port
Username
The database account and password of the PolarDB cluster.
Password
-
-
A successful connection displays the following page.
Notepostgresis the default system database. Do not perform any operations on this database.
Use psql to connect to a cluster
Download psql from PostgreSQL Downloads to connect to a PolarDB cluster. You can also use psql in the to connect to a PolarDB cluster.
-
The connection method is the same for Windows and Linux.
-
The official psql documentation covers detailed usage.
Syntax
psql -h <host> -p <port> -U <username> -d <dbname>
|
Parameter |
Description |
|
|
The cluster endpoint and port of the PolarDB cluster.
|
|
|
|
|
|
The database account of the PolarDB cluster. |
|
|
The . |
Example
Connect to a cluster in a programming language
Connecting to a cluster is similar to connecting to a regular PostgreSQL database — just update the endpoint, port, account, and password.
Java
Connect to a cluster using the PostgreSQL JDBC driver in a Maven-based Java project.
-
Add the PostgreSQL JDBC driver dependency to your pom.xml file. Sample code:
<dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>42.2.18</version> </dependency> -
Connect to the cluster. Replace the
<HOST>,<PORT>,<USER>,<PASSWORD>,<DATABASE>,<YOUR_TABLE_NAME>, and<YOUR_TABLE_COLUMN_NAME>placeholders with the actual cluster connection parameters.import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class PolarDBConnection { public static void main(String[] args) { // Database URL, username, and password. String url = "jdbc:postgresql://<HOST>:<PORT>/<DATABASE>"; String user = "<USER>"; String password = "<PASSWORD>"; try { // Load the PostgreSQL JDBC driver. Class.forName("org.postgresql.Driver"); // Establish the connection. Connection conn = DriverManager.getConnection(url, user, password); // Create a Statement object. Statement stmt = conn.createStatement(); // Execute an SQL query. ResultSet rs = stmt.executeQuery("SELECT * FROM <YOUR_TABLE_NAME>"); // Process the result set. while (rs.next()) { System.out.println(rs.getString("<YOUR_TABLE_COLUMN_NAME>")); } // Close resources. rs.close(); stmt.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } } }
Python
Connect to a cluster using the psycopg2 library in Python 3.
-
Install the psycopg2 library.
pip3 install psycopg2-binary -
Connect to the cluster. Replace the
<HOST>,<PORT>,<USER>,<PASSWORD>,<DATABASE>, and<YOUR_TABLE_NAME>placeholders with the actual cluster connection parameters.import psycopg2 try: # Connection parameters conn = psycopg2.connect( host="<HOST>", # The cluster endpoint. database="<DATABASE>", # The database name. user="<USER>", # The username. password="<PASSWORD>", # The password. port="<PORT>" # The port number. ) # Create a cursor object. cursor = conn.cursor() # Execute a query. cursor.execute("SELECT * FROM <YOUR_TABLE_NAME>") # Get all results. records = cursor.fetchall() for record in records: print(record) except Exception as e: print("Error:", e) finally: # Close the connection. if 'cursor' in locals(): cursor.close() if 'conn' in locals(): conn.close()
Go
Connect to a cluster using the database/sql package and the lib/pq driver in Go 1.23.0.
-
Install the
lib/pqdriver.go get -u github.com/lib/pq -
Connect to the cluster. Replace the
<HOST>,<PORT>,<USER>,<PASSWORD>,<DATABASE>, and<YOUR_TABLE_NAME>placeholders with the actual cluster connection parameters.package main import ( "database/sql" "fmt" "log" _ "github.com/lib/pq" // Initialize the PostgreSQL driver. ) func main() { // The connection string format. connStr := "user=<USER> password=<PASSWORD> dbname=<DATABASE> host=<HOST> port=<PORT> sslmode=disable" // Open a database connection. db, err := sql.Open("postgres", connStr) if err != nil { log.Fatal(err) } defer db.Close() // Close the connection when the program exits. // Test the connection. err = db.Ping() if err != nil { log.Fatal(err) } fmt.Println("Connected to PostgreSQL!") // Execute a query. rows, err := db.Query("SELECT * FROM <YOUR_TABLE_NAME>") if err != nil { log.Fatal(err) } defer rows.Close() }




