Relational Database ServiceRelational Database Service

Compute
Elastic Cloud Server
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
Domain Name Service
VPC Endpoint
Cloud Connect
Enterprise Switch
Security & Compliance
Anti-DDoS
Web Application Firewall
Host Security Service
Data Encryption Workshop
Database Security Service
Advanced Anti-DDoS
Data Security Center
Container Guard Service
Situation Awareness
Managed Threat Detection
Compass
Cloud Certificate Manager
Anti-DDoS Service
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GaussDB NoSQL
GaussDB(for MySQL)
Distributed Database Middleware
GaussDB(for openGauss)
Developer Services
ServiceStage
Distributed Cache Service
Simple Message Notification
Application Performance Management
Application Operations Management
Blockchain
API Gateway
Cloud Performance Test Service
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
DevCloud
ProjectMan
CodeHub
CloudRelease
CloudPipeline
CloudBuild
CloudDeploy
Cloud Communications
Message & SMS
Cloud Ecosystem
Marketplace
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP License Service
Support Plans
Customer Operation Capabilities
Partner Support Plans
Professional Services
enterprise-collaboration
Meeting
IoT
IoT
Intelligent EdgeFabric
DeveloperTools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Help Center> Relational Database Service> User Guide> Working with RDS for PostgreSQL> Instance Connection> Connecting to an RDS for PostgreSQL Instance Through JDBC
Updated at: Apr 02, 2022 GMT+08:00

Connecting to an RDS for PostgreSQL Instance Through JDBC

Although the SSL certificate is optional if you choose to connect to a database through Java database connectivity (JDBC), download an SSL certificate to encrypt the connections for security.

Prerequisites

Familiarize yourself with:

  • Computer basics.
  • Java programming language.
  • JDBC knowledge.

Obtaining and Using JDBC

Connection with the SSL Certificate

Download the SSL certificate and verify the certificate before connecting to databases.

In the DB Information area on the Basic Information page, click in the SSL field to download the root certificate or certificate bundle.

  1. Connect to the RDS PostgreSQL DB instance through JDBC.

    jdbc:postgresql://<instance_ip>:<instance_port>/<database_name>?sslmode=verify-ca&sslrootcert=<ca.pem>
    Table 1 Parameter description

    Parameter

    Description

    <instance_ip>

    If you attempt to access the RDS DB instance through an ECS, set instance_ip to the floating IP address displayed on the Basic Information page of the DB instance to which you intend to connect.

    If you attempt to access the RDS DB instance through an EIP, set instance_ip to the EIP that has been bound to the DB instance.

    <instance_port>

    Enter the database port displayed on the Basic Information page. Default value: 5432

    <database_name>

    Enter the name of the database to which you intend to connect. Default value: postgres

    sslmode

    Enter the SSL connection mode.

    verify-ca: I want my data encrypted, and I accept the overhead. I want to be sure that I connect to a server that I trust.

    For details about other options, see https://jdbc.postgresql.org/documentation/head/ssl-client.html.

    sslrootcert

    Path of the CA certificate for the SSL connection. For details, see https://jdbc.postgresql.org/documentation/head/ssl-client.html.

    Example script in Java:

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    
    public class MyConnTest {
    	final public static void main(String[] args) {
    		Connection conn = null;
    		// set sslmode here.
    		// with ssl certificate and path.
    		String url = "jdbc:postgresql://192.168.0.225:5432/my_db_test?sslmode=verify-ca&sslrootcert=/home/Ruby/ca.pem";
    
    		try {
    			Class.forName("org.postgresql.Driver");
    			conn = DriverManager.getConnection(url, "root", "password");
    			System.out.println("Database connected");
    
    			Statement stmt = conn.createStatement();
    			ResultSet rs = stmt.executeQuery("SELECT * FROM mytable WHERE columnfoo = 500");
    			while (rs.next()) {
    				System.out.println(rs.getString(1));
    			}
    
    			rs.close();
    			stmt.close();
    			conn.close();
    		} catch (Exception e) {
    			e.printStackTrace();
    			System.out.println("Test failed");
    		} finally {
    			// release resource ....
    		}
    	}
    }

Connection Without the SSL Certificate

You do not need to download the SSL certificate because the certificate verification on the server is not required.

  1. Connect to the RDS for PostgreSQL DB instance through JDBC.

    jdbc:postgresql://<instance_ip>:<instance_port>/<database_name>?sslmode=disable
    Table 2 Parameter description

    Parameter

    Description

    <instance_ip>

    If you attempt to access the RDS DB instance through an ECS, set instance_ip to the floating IP address displayed on the Basic Information page of the DB instance to which you intend to connect.

    If you attempt to access the RDS DB instance through an EIP, set instance_ip to the EIP that has been bound to the DB instance.

    <instance_port>

    Enter the database port displayed on the Basic Information page. Default value: 5432

    <database_name>

    Enter the name of the database to which you intend to connect. Default value: postgres

    sslmode

    Enter the SSL connection mode.

    disable: I don't care about security and don't want to pay the overhead for encryption.

    For details about other options, see https://jdbc.postgresql.org/documentation/head/ssl-client.html.

    Example script in Java:

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    
    public class MyConnTest {
    	final public static void main(String[] args) {
    		Connection conn = null;
    		// set sslmode here.
    		// no ssl certificate, so do not specify path.
    		String url = "jdbc:postgresql://192.168.0.225:5432/my_db_test?sslmode=disable";
    		try {
    			Class.forName("org.postgresql.Driver");
    			conn = DriverManager.getConnection(url, "root", "password");
    			System.out.println("Database connected");
    
    			Statement stmt = conn.createStatement();
    			ResultSet rs = stmt.executeQuery("SELECT * FROM mytable WHERE columnfoo = 500");
    			while (rs.next()) {
    				System.out.println(rs.getString(1));
    			}
    			rs.close();
    			stmt.close();
    			conn.close();
    		} catch (Exception e) {
    			e.printStackTrace();
    			System.out.println("Test failed");
    		} finally {
    			// release resource ....
    		}
    	}
    }

Did you find this page helpful?

Failed to submit the feedback. Please try again later.

Which of the following issues have you encountered?







Please complete at least one feedback item.

Content most length 200 character

Content is empty.

OK Cancel