简体   繁体   中英

Cannot connect to Docker PostgreSQL container in AWS EC2 instance

Docker newbie here.

I'm trying to create a docker container for PostgreSQL in AWS EC2 instance.

First, to achieve persistence I use data volume container . Here is the command I used to create a 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 run this in my local environment and then tried to connect to it through a PostgreSQL desktop client and it worked. I was able to connect to the db_name that is stored in local PostgreSQL docker container.

After this, I sshed into one of my EC2 instances in AWS. And followed the same flow I described above. Then returned back to the PostgreSQL desktop client and tried to connect to it. But I got the following error message:

Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.

What am I missing here?

Edit

I've already checked and set the listen_addresses on postgresql.conf file to

listen_addresses='*'

and appended the following line to pg_hba.conf file:

host all all 0.0.0.0/0 md5

The postgresql.conf and pg_hba.conf look good. The docker run correctly publish postgresql port. Check your EC2 inbound rule, probably port 5432 is not allowed to be accessed.

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

listen_addresses = '*'

Also edit pg_hba.conf accordingly.

You need the give the host Id as well. loggin into postges container by docker exec -it /bin/bash

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

add use the host id instead of 0.0.0.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM