Connect to a PostgreSQL instance

更新时间:
复制 MD 格式

After you create an ApsaraDB RDS for PostgreSQL instance and complete the necessary configurations, such as creating an account and setting up an IP whitelist, you can connect to the instance. This topic describes how to connect using Data Management (DMS), a pgAdmin client, the PostgreSQL CLI, or an application.

Prerequisites

  • Create an ApsaraDB RDS for PostgreSQL instance. For more information, see Create an ApsaraDB RDS for PostgreSQL instance.

  • Create an account and a database. For more information, see Create an account and a database.

  • Configure an IP whitelist to allow access from your ECS instance or on-premises device. For more information, see Configure an IP whitelist.

    • If connecting from an ECS instance over an internal network, both the ECS and RDS instances must be in the same region, Alibaba Cloud account, and VPC. You must add the private IP address of the ECS instance to the IP whitelist.

    • If you connect from an on-premises device, add the public IP address of the device to the IP whitelist.

Procedure

Data Management (DMS)

Data Management (DMS) is an all-in-one data management service that integrates data management, schema management, user authorization, security auditing, data trends, data tracing, BI charts, performance optimization, and server management. For more information about DMS, see What is Data Management (DMS)?

  1. Log on to the ApsaraDB RDS console and go to the Instances page. In the top navigation bar, select the region in which the RDS instance resides. Then, find the RDS instance and click the instance ID.

  2. On the Basic Information page, click Log On to Database.SQLQuery

  3. On the Log on to database instance page in DMS, select an Access Method and a Control Mode.

    Parameter

    Description

    Access Method

    The method to access the ApsaraDB RDS instance by using DMS. This topic uses Account + Password Login as an example.

    • Account + Password Login: Log on with the username and password of a database account that has permissions for the target database.

    • Log on with KMS credential: DMS automatically enables secure hosting for the instance. You must manually select the RDS credential created in KMS to log on.

    Note
    • When using the Log on with KMS credential access method, DMS automatically enables secure hosting for the instance at no cost.

    • You can also click One-click enable secure hosting, enter an account and password, and enable secure hosting for the instance for free. This enables secure, controllable, and password-free logon.

    Control Mode

    DMS provides three instance-level control modes. You can choose one based on your business scenario.

    • Flexible Management

    • Stable Change

    • Security Collaboration

  4. After you log on and refresh the page, the instances and their databases appear in the Instances Connected pane on the left.

    Note

    If you enabled secure hosting, you can see the logged-in instances and their databases in the Password-free instances pane on the left.

    image

    • In the DMS console, you can only switch databases by double-clicking the target database. The USE command is not supported.

    • If the instance exists but you cannot find the target database after expanding it, the cause may be one of the following:

      • The logon account does not have access permissions for the target database. Log on with an account that has the required permissions.

      • The catalog is not displayed because metadata is not synchronized. To resolve this, hover over the instance name and click the image icon to its right. This refreshes the database list and displays the target database.

      To quickly synchronize the schemas of your databases and tables, you can use the DMS empty database initialization feature.

Note
  • In addition to logging on to DMS from the ApsaraDB RDS console, you can also log on to the DMS console and add your ApsaraDB RDS instance directly. Then, you can quickly log on to your database from the DMS console. For more information, see Register a cloud database instance.

  • If you reset your account password, you must log on to DMS again.

pgAdmin client

The pgAdmin client is the official recommended tool for connecting to PostgreSQL databases. This section uses pgAdmin 4 v6.2.0 as an example to show how to connect to an ApsaraDB RDS for PostgreSQL instance. When you download and install PostgreSQL from the official PostgreSQL website, the pgAdmin 4 client is automatically installed.

If you do not want to install PostgreSQL, you can download the standalone pgAdmin client for remote connections only.

  1. Start the pgAdmin 4 client.

    Note

    When you log on to a recent version of the client for the first time, you must set a Master Password to protect saved passwords and other credentials.

  2. Right-click Servers and choose Register > Server....

  3. On the General tab, set a name for the connection.

  4. Click the Connection tab and enter the connection information for your instance.

    Parameter

    Description

    Host name/address

    The endpoint and port of the ApsaraDB RDS for PostgreSQL instance.

    • To connect over an internal network, enter the internal endpoint and port of the instance.

    • To connect over the internet, enter the public endpoint and port of the instance.

    You can view this information on the Database Connection page of your ApsaraDB RDS for PostgreSQL instance.

    For more information, see View and change endpoints and ports.

    Port

    Username

    The username and password of your ApsaraDB RDS for PostgreSQL instance account.

    For information about how to create an account, see Create an account and a database.

    Password

  5. Click Save.

    If the connection information is correct, the server is successfully added.

    Important

    The postgres database is the default system database for the ApsaraDB RDS instance. Do not perform any operations in this database.

PostgreSQL CLI

When you install PostgreSQL from the official PostgreSQL website, the PostgreSQL CLI is also installed.

In the command-line terminal, run the following command to connect to your ApsaraDB RDS for PostgreSQL database.

psql -h <endpoint> -U <username> -p <port> [-d <database_name>]

psqlLog on

Parameter

Description

Endpoint

The endpoint and port of the ApsaraDB RDS for PostgreSQL instance.

  • To connect over an internal network, enter the internal endpoint and port of the instance.

  • To connect over the internet, enter the public endpoint and port of the instance.

You can view this information on the Database Connection page of your ApsaraDB RDS for PostgreSQL instance.

For more information, see View and change endpoints and ports.

Port Number

Username

The account for your ApsaraDB RDS for PostgreSQL instance.

For information about how to create an account, see Create an account and a database.

Database Name

Optional. The name of the database to connect to. The postgres database is the default system database for the ApsaraDB RDS for PostgreSQL instance. Do not perform any operations in this database. We recommend that you specify another database that you created on the instance.

For information about how to create and view databases, see Create a database.

Application

Note

This section provides an example of how to connect to an ApsaraDB RDS for PostgreSQL instance from a Maven project using JDBC. The connection process is similar for other programming languages.

  1. Add the dependency to the pom.xml file.

    <dependency>
      <groupId>postgresql</groupId>
      <artifactId>postgresql</artifactId>
      <version>8.2-504.jdbc3</version>
    </dependency>
  2. The following code provides an example of how to connect to the instance:

    public class DatabaseConnection
    {
        public static void main( String[] args ){
            try {
                Class.forName("org.postgresql.Driver");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            // The endpoint of the instance.
            String hostname = "pgm-bp1i3kkq7321o9****.pg.rds.aliyuncs.com";
            // The port of the instance.
            int port = 5432;
            // The name of the database.
            String dbname = "postgres";
            // The username.
            String username = "username";
            // The password.
            String password = "password";
    
            String dbUrl = "jdbc:postgresql://" + hostname + ":" + port + "/" + dbname + "?binaryTransfer=true";
            Connection dbConnection;
            try {
                dbConnection = DriverManager.getConnection(dbUrl, username, password);
                Statement statement = dbConnection.createStatement();
                // Enter the SQL statement to be executed.
                String selectSql = "SELECT * FROM information_schema.sql_features LIMIT 10";
                ResultSet resultSet = statement.executeQuery(selectSql);
                while (resultSet.next()) {
                    System.out.println(resultSet.getString("feature_name"));
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

Third-party BI tool

Microsoft Power BI

ApsaraDB RDS for PostgreSQL supports connections from Power BI. You can use Power BI to get, clean, model, and visualize data for analysis. This section shows you how to connect to an ApsaraDB RDS for PostgreSQL instance by using Power BI 2.112.1161.0 64-bit as an example.

  1. Download and install the Power BI Desktop client. For more information, see Get Power BI Desktop.

  2. Start the Power BI Desktop client.

  3. On the Homepage tab in the top menu bar, click Get data > More....Get data

  4. In the Get Data dialog box, select Database > PostgreSQL, and then click Connection.PostgreSQLDatabase

  5. In the PostgreSQL dialog box, specify the Server and Database, and then click Yes.Connection address

    Parameter

    Description

    Server

    The endpoint and port of the ApsaraDB RDS for PostgreSQL instance.

    Format: endpoint:port

    • To connect over an internal network, enter the internal endpoint and port of the instance.

    • To connect over the internet, enter the public endpoint and port of the instance.

    You can view this information on the Database Connection page of your ApsaraDB RDS for PostgreSQL instance.

    For more information, see View and change endpoints and ports.

    Database

    The postgres database is the default system database for the ApsaraDB RDS for PostgreSQL instance. Do not perform any operations in this database. We recommend that you specify another database that you created on the instance.

    For information about how to create and view databases, see Create a database.

  6. Set the Username and Password, and then click Connection.Connect

    Note

    For information about how to create an account, see Create an account and a database.

  7. In the Encryption Support dialog box, click Yes.Encryption Support

  8. After a successful connection, you can view table information in the database from the Navigator window. You can select a target table and then choose Load or Transform Data based on your business requirements.Load or Transform Data

Smartbi

ApsaraDB RDS for PostgreSQL supports connecting to Smartbi for enterprise-level reporting, data visualization, self-service exploratory analysis, data mining, and AI-powered analytics. This section uses Smartbi V10.5 as an example to demonstrate the connection process.

  1. Follow the official Smartbi installation guide to apply for a license, download and install the Smartbi client, and start the Smartbi service.

  2. From the Start menu, click Access Smartbi.

  3. Enter your account and password, and then click Login.

    Note
    • The default administrator account is admin, and the password is manager.

    • If this is your first time logging in, you must change the administrator password.

  4. In the left-side navigation pane, click the icon and select Database Connection > Create > Relational Database.

  5. In the New Relational Database dialog box, configure the parameters, then click Test Connection (T).

    Parameter

    Description

    Name

    The name for the database connection. This is user-defined.

    Driver type

    Select PostgreSQL.

    Driver class

    This is automatically selected after you choose the Driver type. No changes are needed.

    Connection Character String

    The JDBC URL to connect to the ApsaraDB RDS for PostgreSQL instance, in the following format:

    jdbc:postgresql://<servername>:<port>/<dbName>?defaultRowFetchSize=10000
    • <servername>:<port>: The endpoint and port of the ApsaraDB RDS for PostgreSQL instance.

      • To connect over an internal network, enter the internal endpoint and port of the instance.

      • To connect over the internet, enter the public endpoint and port of the instance.

      You can view this information on the Database Connection page of your ApsaraDB RDS for PostgreSQL instance.

      For more information, see View and change endpoints and ports.

    • <dbName>: The postgres database is the default system database. Do not perform any operations in this database.

      We recommend that you specify another database that you created on the ApsaraDB RDS instance. For information about how to create and view databases, see Create a database.

    Username, Password

    For information about how to create an account for your ApsaraDB RDS instance, see Create an account.

  6. If the test passes, click Save. A success message appears.

SSL encryption

ApsaraDB RDS for PostgreSQL supports SSL encryption to protect data in transit. For specific configuration details, see Connect to an ApsaraDB RDS for PostgreSQL instance over an SSL-encrypted connection.

Troubleshoot connection failures

For more information, see Troubleshoot issues that prevent you from connecting to an ApsaraDB RDS instance.

FAQ

  • Q: How do I connect to an ApsaraDB RDS instance if my ECS and ApsaraDB RDS instances are in different regions or in different accounts?

    A: You can use one of the following two methods:

  • Q: How can I connect to ApsaraDB RDS from Function Compute to retrieve data?

    A: You can install third-party dependencies for your function and use built-in modules to retrieve data from ApsaraDB RDS. For more information, see Install third-party dependencies for a function.