0% found this document useful (0 votes)
33 views

Deploy RDS@PostgreSQL-16 & Connect Via PSQL From Another Host

This document provides steps to launch an RDS Postgres instance and connect to it from an EC2 instance. It includes launching the RDS instance, configuring security groups, connecting via psql, and setting the Postgres password. Scripts are also provided to install Postgres 16 on the EC2 instance and configure it to allow connections from the EC2 host IP.

Uploaded by

Vadivel Rajavel
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Deploy RDS@PostgreSQL-16 & Connect Via PSQL From Another Host

This document provides steps to launch an RDS Postgres instance and connect to it from an EC2 instance. It includes launching the RDS instance, configuring security groups, connecting via psql, and setting the Postgres password. Scripts are also provided to install Postgres 16 on the EC2 instance and configure it to allow connections from the EC2 host IP.

Uploaded by

Vadivel Rajavel
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Launch RDS postgres compatible instance ( Free tier )

Validate the Security group has incoming port to Ec2 having psql

Connect using psql with postgres role to RDS

Set the password like this (YPdhPmKhfvXXeKx16gcM ) for simplicity else keep
autogenerated

keep the endpoint as target

Launch Ec2 target to connect from

sudo su -

vi /tmp/install_source_pg16.sh

#!/bin/sh
echo "Start of the script . It installs postgres 16 & adds postgres OS user to sudoers list
"
echo " updating hostname in /etc/hosts"
echo " replace below IP with Private IP of EC2 INSTANCE "
export source_IP="172.31.41.251"
#export target_IP="172.31.46.96"
echo "$source_IP source" >> /etc/hosts
echo "$target_IP target" >> /etc/hosts
echo " Setting hostname of source host"
sudo hostnamectl set-hostname source
echo " Installing postgres 16"
sudo sh -c 'echo "deb https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg
main" > /etc/apt/sources.list.d/pgdg.list'
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key
add -
sudo apt-get update
sudo apt-get -y install postgresql-16
echo " Add postgres OS user to sudoers file "
echo "postgres ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
echo " end of the installation script"
echo " execute the script to install binaries "

sh -x /tmp/install_source_pg16.sh
sudo su - postgres

vi /tmp/postgres_setting_pg_source.sh

echo "Start of the script /tmp/postgres_setting_pg_source.sh"


#!/bin/sh
echo"start of the postgres_setting_pg_source.sh script"
export source_IP="172.31.41.251"
#export target_IP="172.31.46.96"
#export client_IP="171.76.85.173"
echo " Enable remote login so you can connect from a particular IP"
grep listen /etc/postgresql/16/main/postgresql.conf
sed -i 's/localhost/*/' /etc/postgresql/16/main/postgresql.conf
sed -i 's/#listen_addresses/listen_addresses/' /etc/postgresql/16/main/postgresql.conf
grep listen /etc/postgresql/16/main/postgresql.conf
cat /etc/postgresql/16/main/pg_hba.conf
sudo systemctl restart postgresql.service
echo "Update the hba.conf file "
psql -c "alter USER postgres PASSWORD 'postgres';"
psql -c "create role client_user with login password 'password' ;"
#echo "host all all $target_IP/32 md5" >>
/etc/postgresql/16/main/pg_hba.conf
echo "host all all $source_IP/32 md5" >>
/etc/postgresql/16/main/pg_hba.conf
#echo "host all all $client_IP/32 md5" >>
/etc/postgresql/16/main/pg_hba.conf
psql -c "SELECT pg_reload_conf();"
sudo systemctl restart postgresql
psql -c "SELECT pg_reload_conf();"
echo "End of script /tmp/postgres_setting_pg_source.sh"

sudo su - postgres

sh -x /tmp/postgres_setting_pg_source.sh

echo " source=> Testing connectivity from VM to RDS"

telnet RDS_ENDPOINT 5432

telnet target.cuw6dvabjk5x.ap-south-1.rds.amazonaws.com 5432

Note : Ensure RDS must have SG with inbound rules allow for port 5432 to SG of
Ec2

shell

export PGPASSWORD=YPdhPmKhfvXXeKx16gcM

psql -h target.cuw6dvabjk5x.ap-south-1.rds.amazonaws.com -d postgres -U postgres

You might also like