Connect the AWS Relational Database Service (RDS) from EC2 Instance

Amol Gunjal
Nerd For Tech
Published in
2 min readFeb 1, 2021

--

Creating an EC2 instance and connecting the RDS PostgreSQL DB instance.

Considering the below instances are already created,
1. RDS PostgreSQL DB instance ( Refer to this article)
2. EC2 Instance

Connect to EC2 instance using ssh client:

1. Open an SSH client.
2. Locate your private key file. The key used to launch this instance is {permissionkey}.pem
3. Connect to your instance using its Public DNS: {public dns}
Example:
ssh -i “{permissionkey}.pem” ubuntu@{public dns}

Once the ssh client is connected, then update OS and install the PostgreSQL client.

sudo apt-get update && sudo apt-get upgrade
sudo apt-get purge postgresql*
sudo apt-get -f install
sudo apt-get install postgresql

Connect PostgreSQL client,

root@ip-{ip address}:/home/ubuntu# psql — host={RDS Endpoint} — port=5432 — username={user name of RDS DB instance} — password — dbname=test_db

Then, enter the password of the RDS DB instance

Once connected to the PostgreSQL client, you will be able to access the test_db database.

test_db=> create table departments(id integer, name varchar);
CREATE TABLE

test_db=> INSERT INTO departments(id, name) values (1, ‘IT’);
INSERT 0 1

test_db=> SELECT * FROM departments;
id | name
— — + — — —
1 | IT
(1 row)

test_db=> create table users(id integer, name varchar, department varchar);
CREATE TABLE

test_db=> INSERT INTO users(id, name, department) values(1, ‘Amol’, 1);
INSERT 0 1

test_db=> SELECT * FROM users;
id | name | department
— — + — — — + — — — — — —
1 | Amol | 1
(1 row)

You can connect RDS DB Instance using the DB Tool as well, https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_GettingStarted.CreatingConnecting.PostgreSQL.html

--

--