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
Connecting to the PSQL server via CLI : Run the below command to enter into the container (with the ID from step-1). docker exec -it <PSQL-Container-ID> bash. Authenticate to start using as postgres user. psql -h localhost -p 5432 -U postgres -W.
To install Docker on an Amazon EC2 instanceConnect to your instance using SSH. For more information, see Connect to your Linux instance using SSH in the Amazon EC2 User Guide for Linux Instances. Update the installed packages and package cache on your instance. Install the most recent Docker Engine package.
Amazon ECS is a highly scalable, high performance container management service that supports Docker containers and allows you to easily run applications on a managed cluster of Amazon EC2 instances.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With