• Relational Database Service

rds
  1. Help Center
  2. Relational Database Service
  3. User Guide
  4. FAQs
  5. Connection and Access FAQs
  6. How Can I Connect to a PostgreSQL Database Through JDBC?

How Can I Connect to a PostgreSQL Database Through JDBC?

If you are connecting to a PostgreSQL database through Java database connectivity (JDBC), the SSL certificate is optional. For security reasons, you are advised to download the SSL certificate to encrypt the connection.

Prerequisites

You are familiar with basic computer knowledge, Java, and JDBC.

Connection Without the SSL Certificate

NOTE:

The JDBC connection is an SSL connection, but you do not need to download the SSL certificate because the certificate verification on the server is not required.

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

    jdbc:postgresql://<instance_ip>:<instance_port>/<database_name>?sslmode=require
    NOTE:
    • If you are accessing the RDS DB instance through an ECS, instance_ip indicates the floating IP address displayed on the Basic Information page of the DB instance to which you intend to connect. If you are accessing the RDS DB instance through an EIP, instance_ip indicates the EIP displayed on the Basic Information page of the DB instance to which you intend to connect.
    • instance_port indicates the database port number displayed on the Basic Information page. The default port number is 5432.
    • database_name indicates the name of the database to which you intend to connect. The default database name is postgres.
    • sslmode indicates the SSL connection mode. require indicates that data needs to be encrypted.

    Example java scripts:

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.Statement;
    
    public class pgsql{  
            final public static void main(String args[]) {  
                Connection c = null;
                Statement stmt = null;
                try {
                    Class.forName("org.postgresql.Driver");
                    c = DriverManager.getConnection("jdbc:postgresql://192.168.0.225:5432/pgtest?sslmode=require", "root", "");
                    c.setAutoCommit(false);
                    System.out.println("Opened database successfully");
                    
                    stmt = c.createStatement();
                    String sql = "CREATE TABLE STUDENTS (ID TEXT PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, SEX TEXT NOT NULL, AGE TEXT NOT NULL)";
                    stmt.executeUpdate(sql);
                    System.out.println("Table created successfully");
                    
                    stmt.close();
                    c.commit();
                    c.close();
                } catch (Exception e){
                    System.err.println(e.getClass().getName() + ": " + e.getMessage());
                    System.exit(0);
                }
            }  
        }

Connection with the SSL Certificate

NOTE:

The JDBC connection is an SSL connection. The SSL certificate needs to be downloaded and verified for connecting to databases.

For details about how to download the SSL certificate, see section Connecting to a DB Instance Through psql.

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

    jdbc:postgresql://<instance_ip>:<instance_port>/<database_name>?sslmode=verify-full&sslrootcert=<ca.pem>
    NOTE:
    • If you are accessing the RDS DB instance through an ECS, instance_ip indicates the floating IP address displayed on the Basic Information page of the DB instance to which you intend to connect. If you are accessing the RDS DB instance through an EIP, instance_ip indicates the EIP displayed on the Basic Information page of the DB instance to which you intend to connect.
    • instance_port indicates the database port number displayed on the Basic Information page. The default port number is 8635.
    • database_name indicates the name of the database to which you intend to connect. The default database name is postgres.
    • sslmode indicates the SSL connection mode. The default mode is verify-full.
    • 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.

    Example java scripts:

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.Statement;
    
    public class pgsql{  
            final public static void main(String args[]) {  
                Connection c = null;
                Statement stmt = null;
                try {
                    Class.forName("org.postgresql.Driver");
                    c = DriverManager.getConnection("jdbc:postgresql://192.168.0.225:5432/pgtest?sslmode=verify-full&sslrootcert=/home/Ruby/ca.pem", "root", "");
                    c.setAutoCommit(false);
                    System.out.println("Opened database successfully");
                    
                    stmt = c.createStatement();
                    String sql = "CREATE TABLE STUDENTS (ID TEXT PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, SEX TEXT NOT NULL, AGE TEXT NOT NULL)";
                    stmt.executeUpdate(sql);
                    System.out.println("Table created successfully");
                    
                    stmt.close();
                    c.commit();
                    c.close();
                } catch (Exception e){
                    System.err.println(e.getClass().getName() + ": " + e.getMessage());
                    System.exit(0);
                }
            }  
        }