1

Spent quite sometime on this but no breakthrough. This is my container creation statement:

> docker create --name aasaan_db_dev -e POSTGRES_USER=aasaan -e
> POSTGRES_PASSWORD=aasaan_admin --expose 5432 postgres:9.4.0
> 
> > deepakkt@deepakkt-ubuntu:~$ docker ps
> 
> CONTAINER ID        IMAGE               COMMAND                 
> CREATED             STATUS              PORTS               NAMES
> 15b21deb19a0        postgres:9.4.0      "/docker-entrypoin..."   About
> an hour ago   Up About an hour    5432/tcp            aasaan_db_dev

The following works:

> deepakkt@deepakkt-ubuntu:~$ docker exec -it aasaan_db_dev psql -U
> aasaan psql (9.4.0) Type "help" for help.
> 
> aasaan=#

However, from the host (same error from pgAdmin as well)

> deepakkt@deepakkt-ubuntu:~$ psql -h 127.0.0.1 -U aasaan 
Password for user aasaan:  
psql: FATAL:  password authentication failed for user
> "aasaan" FATAL:  password authentication failed for user "aasaan"

Contents of pg_hba.conf inside the container:

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local   replication     postgres                                trust
#host    replication     postgres        127.0.0.1/32            trust
#host    replication     postgres        ::1/128                 trust

host all all 0.0.0.0/0 md5

What am I missing?

0

3 Answers 3

2

It is needed to map the container port to host port.

Have a look at below docker documentation:

The EXPOSE instruction does not actually publish the port. It functions as a type of documentation between the person who builds the image and the person who runs the container, about which ports are intended to be published. To actually publish the port when running the container, use the -p flag on docker run to publish and map one or more ports, or the -P flag to publish all exposed ports and map them to high-order ports.

Source: docker docs

Sign up to request clarification or add additional context in comments.

1 Comment

your answer helped me. However in its current state it is partial. If you can update the answer with the proper command, I will accept it. What happened was that an earlier installtion on host was conflicting with port 5432. I cleaned that up and did -P as you mentioned and it worked!
1

docker ps is showing 5432/tcp for your port section. This means the port 5432 is opened on your postgres container. (This is by default because it's exposed in the Dockerfile of postgres so you don't need the --expose command).

The name of your container is aasaan_db_dev. Other containers which are deployed in the same bridge network can access your container using aasaan_db_dev:5432.

But your container is not mapping it's port to the outside which means you can not access the container from outside the bridge network. (for example not from eth0).

To map your port use the following command:

docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d -p 5432:5432 postgres

In this case the -p option will map 5432 from your container on 5432 of your host. Now you should be able to connect to the container from your host network (for example localhost).

1 Comment

noted. i had another issue wherein a local (non-docker) install of postgres was interfering with port 5432 as well. it thought i had removed it but it was still lurking around. cleaning that up and running with -p worked!
0

You should not use 127.0.0.1 as it will refer to your localhost. But actually, it is running on a container.

You should use

psql -h <imagename> -U <username> 

You need to provide port as well.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.