Create two EC2 instances

Install postgres on ubuntu for both EC2 instances

sudo apt-get update
sudo apt-get install postgresql postgresql-contrib
sudo -i -u postgres
# Create db with owner on test1 instance
createdb -O postgres test1

Set local db

Step 1. modify pg_hba.conf

sudo vim /etc/postgresql/9.3/main/pg_hba.conf
# change following line
local  all  postgres  peer
to
local  all  postgres  trust

# change following line
local  all  all       peer
to
local  all  all       trust

Step 2. test

sudo /etc/init.d/postgresql restart
psql -U postgres test1

Let Remote instance can access

Step 1. modify pg_hba.conf

sudo vim /etc/postgresql/9.3/main/pg_hba.conf
# add following line
host  all  all   [test2's ip]/24 trust

Step 2. modify postgresql.conf

sudo vim /etc/postgresql/9.3/main/postgresql.conf
# change following line
listen_addresses = 'localhost'
to
listen_addresses = '*' or listen_addresses = 'test2's ip'

Step 3. modify security group on AWS

  • select security group of test1
  • add new inbound rule for postgres
PostgreSQL / TCP / 5432 / Customer IP / test2's ip

Step 4. restart postgres

sudo /etc/init.d/postgresql restart

Final

  • login to test2 instance
  • sudo -i -u postgres
psql -h [test1's ip] -p 5432 -U postgres test1

Reference