Can't connect to PostgreSQL Docker container on AWS EC2 instance

Docker newbie here.

I am trying to create a Docker container for PostgreSQL on an AWS EC2 instance.

First, I use a data container to achieve persistence . Here is the command I used to create the data volume container for my PostgreSQL database:

docker create -v /var/lib/postgresql/data --name postgres9.3.6-data busybox

      

Then I run the following command to create the PostgreSQL container:

docker run --name postgres9.3.6 \
-p 5432:5432 \
-e POSTGRES_PASSWORD=password \
-e POSTGRES_USER=root \
-e POSTGRES_DB=db_name \
-d --volumes-from postgres9.3.6-data postgres:9.3.6

      

I ran this in my local environment and then tried to connect to it via the PostgreSQL desktop client and it worked. I was able to connect to the db_name which is stored in the local PostgreSQL docker container.

After that, I snuck into one of my EC2 instances on AWS. And the same stream followed, which I described above. Then I went back to the PostgreSQL desktop client and tried to connect to it. But I got the following error message:

The connection was rejected. Make sure the hostname and port are correct and that the postmaster is accepting TCP / IP connections.

What am I missing here?

Edit

I already checked and set listen_addresses in postgresql.conf file to

listen_addresses='*'

      

and added the following line to pg_hba.conf file:

host all all 0.0.0.0/0 md5

      

+3


source to share


3 answers


Postgresql.conf and pg_hba.conf looks good. Docker runs postgresql publish port correctly. Check the inbound EC2 rule, it may be denied access to ports 5432.



+1


source


You should edit postgresql.conf, set listen_addresses parameter to '*':

listen_addresses = '*'



Also edit the pg_hba.conf file accordingly.

0


source


You also need to provide the host ID. login to postges container with docker exec -it / bin / bash

open the host file and the last line in the file is the host id. the file is available in the etc directory and the file name is host.

add use host id instead of 0.0.0.0

0


source







All Articles