• Relational Database Service

rds
  1. Help Center
  2. Relational Database Service
  3. User Guide
  4. Getting Started with PostgreSQL
  5. Connecting to a DB Instance Through psql

Connecting to a DB Instance Through psql

You can use the PostgreSQL client psql to connect to a DB instance through a common connection or an SSL connection. The SSL connection is encrypted and therefore more secure.

Preparations

  1. Prepare an ECS or a device that can access RDS DB instances.
    • To connect to a DB instance through an ECS, you must first create an ECS.

      For details about how to create and connect to an ECS, see section How Can I Create and Connect to an ECS?

    • To connect to a DB instance through an EIP, you must:
      1. Bind the EIP to the DB instance. For details, see Binding an EIP.
      2. Ensure that the device can access the EIP that has been bound to the DB instance.
  2. Install the PostgreSQL client psql on the ECS or device that was prepared in 1. Prepare an ECS or a d....

    For details, see section How Can I Install the PostgreSQL Client?

Common Connection

  1. Log in to the ECS or device that can access RDS.
  2. Run the following command to connect to an RDS DB instance:

    psql --no-readline -U <user> -h <host> -p <port> -d <datastore> -W

    • The parameter -U indicates the username of the RDS database account. The default administrator is root.
    • The parameter -h indicates different values depending on how you intend to access the DB instance. If you intend to access the DB instance through an ECS, -h indicates the IP address of the primary DB instance. To obtain this IP address, go to the Instance Management page and click the target DB instance. The IP address can be found in the Floating IP Address field on the Basic Information page. If you intend to access the DB instance through an EIP, -h indicates the EIP displayed in the EIP field on the Basic Information page.
    • The parameter -p indicates the database port in use. The default value is 5432. To obtain the database port, go to the Instance Management page and click the target DB instance. The port number can be found in the Database Port field on the Basic Information page.
    • The parameter -d indicates the name of the database (the default database name is postgres).
    • The parameter -W indicates that a password must be entered for the connection. After running this command, you will be prompted to enter a password.

    Example:

    Run the following command as user root to connect to a DB instance:

    psql --no-readline -U root -h 192.168.0.44 -p 5432 -d postgres -W

SSL Connection

  1. On the Instance Management page, click the target DB instance. On the displayed Basic Information page, click Download Certificate in the SSL field to download the root certificate or certificate bundle.
  2. Upload the root certificate to the ECS or save it to the device to be connected to the DB instance.
  3. Run the following command to connect to an RDS DB instance. The Linux OS is used as an example.

    psql --no-readline -h <host> -p <port> "dbname=<database> user=<user> sslmode=verify-ca sslrootcert=<ca.pem>"

    • The parameter -h indicates different values depending on how you intend to access the DB instance. If you intend to access the DB instance through an ECS, -h indicates the IP address of the primary DB instance. To obtain this IP address, go to the Instance Management page and click the target DB instance. The IP address can be found in the Floating IP Address field on the Basic Information page. If you intend to access the DB instance through an EIP, -h indicates the EIP displayed in the EIP field on the Basic Information page.
    • The parameter -p indicates the database port in use. The default value is 5432. To obtain the database port, go to the Instance Management page and click the target DB instance. The port number can be found in the Database Port field on the Basic Information page.
    • The parameter dbname indicates the name of the database (the default database name is postgres).
    • The parameter user indicates the username of the RDS database account. The default administrator is root.
    • The parameter sslmode indicates the SSL connection mode. Set it to verify-ca to use a CA to check whether the service is trusted.
    • The parameter sslrootcert indicates the directory of the CA certificate for the SSL connection. The certificate should be stored in the directory where the command is executed.

    Enter the password of the database account if the following information is displayed:

    Password:

    For example, to connect to a DB instance through an SSL connection as user root, run the following command:

    psql --no-readline -h 192.168.0.44 -p 5432 "dbname=postgres user=root sslmode=verify-ca sslrootcert=/root/ca.pem"

    Password:

  4. The SSL connection is established if information similar to the following is displayed after you log in to the database:

    SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)