Connect to Docker Postgres Container got connection timeout - postgresql

So what i did is:
docker run -d -e POSTGRES_USER=user -e POSTGRES_PASSWORD=456789 --name admin-service -p 5432:5432 postgres
and when i check with docker ps and docker inspect
"Networks": {
...
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
...
}
}
and docker ps:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6f1b75bed5b1 postgres "docker-entrypoint.s…" 18 minutes ago Up 18 minutes 0.0.0.0:5432->5432/tcp admin-service
i went to PgAdmin and try to connect with host: 172.17.0.2, i get connection time out, so i change hostname to localhost, it keep saying password authentication failed, i filled username and password as above. So i have no idea what i did wrong here.

found the problem, just change to another port, and it worked.
docker run --name admin-service -e POSTGRES_PASSWORD=456789 -p 8001:5432 -d postgres

Related

Attempt to connect from SpringBoot to docker-postgres failed

I have created a docker container based on a postgres image.
Which I try to connect to from a Spring Boot application with no success.
The user is 'postgres', and the password 'password'.
(I don't have anything from Docker Compose, nor do I know if it's necessary.)
I would appreciate any help
Finally solved:
Remove all previous containers.
Create new container. This time using port 5433 and expecifying the postgres user.
docker run --name container-postgres-1 -p 5433:5432 -v "C:\volumen-docker-1:/var/lib/postgresql/data" -e POSTGRES_PASSWORD=password -e POSTGRES_USER=postgres -d postgres:13.9-alpine3.17
Create the database bootifyone.
docker exec -it container-postgres-1 bash
psql -h localhost -p 5432 -U postgres
CREATE DATABASE bootifyone;
Using this connection URL in the application.properties:
spring.datasource.url=jdbc:postgresql://localhost:5433/bootifyone

Cannot connect to postgres running in Docker

I am on a Mac.
I pulled the postgres docker image by running:
docker pull postgres:12.4
Then I start the container by running:
docker run --rm --name pg-docker -e POSTGRES_PASSWORD=pass -d -p 5432:5432 -v $HOME/docker/volumes/postgres:/var/lib/postgresql/data postgres
I can confirm the container is running:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6ef29362b6f3 postgres "docker-entrypoint.s…" 4 minutes ago Up 4 minutes 0.0.0.0:5432->5432/tcp pg-docker
I installed only psql on the host and when I try to connect to the postgres, I get the following error:
psql -h localhost -U postgres -d postgres
psql: error: could not connect to server: FATAL: password authentication failed for user "postgres"
password retrieved from file "/Users/me/.pgpass"
I am actually following the steps as outline in this post
I am not sure what is going wrong or how to fix this. Any ideas?

Access Postgres database container but get error

I use docker to run a Postgres database container. My command to start it is:
docker run --name postgres-me -e POSTGRES_PASSWORD=password -d -p 5432:5432 postgres:alpine
Then it is up and running:
~ $ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b662971d9b54 postgres:alpine "docker-entrypoint.s…" 6 seconds ago Up 3 seconds 0.0.0.0:5432->5432/tcp postgres-me
Then I try to access the container by:
~ $ docker exec -it b662971d9b54 bin/bash
bash-5.0# psql
psql: error: could not connect to server: FATAL: role "root" does not exist
As you can see above, after I go into the container, then I tried command psql, but I got error could not connect to server: FATAL: role "root" does not exist
How to get rid of this error?
You need to provide username as well.
Try this :
psql -U <username>

Accessing PostgreSQL on docker container from pgAdmin4 in another docker container

I have a PostgreSQL instance running on a docker with these commands:
mkdir -p $HOME/vols/postgres
docker pull postgres:12.0
docker run --rm --name pg-docker -e POSTGRES_PASSWORD=docker \
-v $HOME/vols/postgres:/var/lib/postgresql/data \
-d -p 5432:5432 postgres
It's up and running and I can access it from DBeaver which is installed on my local-machine. Also, I've installed pgAdmin4 by these commands:
mkdir -p $HOME/vols/pgadmin4
docker pull dpage/pgadmin4
docker run --rm --name pgadmin4 -p 5050:80 \
-v $HOME/vols/pgadmin4:/var/lib/pgadmin \
-e 'PGADMIN_DEFAULT_EMAIL=amiry#manexapp.com' \
-e 'PGADMIN_DEFAULT_PASSWORD=12345678' \
-d dpage/pgadmin4
The pgAdmin is also up and running well and I can easily access it and login to it through http://localhost:5050.
But when I want to connect to my postgre-container instance via pgAdmin4-container instance, I get this error:
Unable to connect to server:
could not connect to server: Connection refused Is the server running
on host "localhost" (127.0.0.1) and accepting TCP/IP connections on
port 5432? could not connect to server: Address not available Is the
server running on host "localhost" (::1) and accepting TCP/IP
connections on port 5432?
Does anybody has an idea what's going wrong here please? Thanks in advance.
NOTE: My host machine is Fedora 31.
Inside a container, the loopback address (localhost or 127.0.0.1) refers to "this container". When you try to connect to 127.0.0.1 inside the pgAdmin4 container, it fails because your Postgres service is not running inside the pgAdmin4 container.
The easiest way to make this work is to put both of your containers on a user defined network, in which case they can simply refer to each other by name.
Start by creating a network:
docker network create dbnet
Then launch the postgres container on that network:
docker run --rm --name pg-docker -e POSTGRES_PASSWORD=docker \
--net dbnet \
-v $HOME/vols/postgres:/var/lib/postgresql/data \
-d -p 5432:5432 postgres
And finally launch the pgAdmin4 container on that network:
docker run --rm --name pgadmin4 -p 5050:80 \
--net dbnet \
-v $HOME/vols/pgadmin4:/var/lib/pgadmin \
-e 'PGADMIN_DEFAULT_EMAIL=amiry#manexapp.com' \
-e 'PGADMIN_DEFAULT_PASSWORD=12345678' \
-d dpage/pgadmin4
Now when you access your pgadmin ui, you can connect to the host pg-docker instead of localhost.
UPDATE: OK. larsks added a full detailed answer with creating a custom network approach, which makes more sense. I'll try it and let you know.
OK. I recognized there is a network problem and pgAdmin4-docker cannot see pg12-docker. So, I did these steps to solve the problem:
A. Stop pgadmin4 container by:
docker stop pgadmin4
This will also delete the container, because I have ran it by --rm flag.
B. Find the network-name and IP associated to pg12-docker by running:
docker inspect pg12-docker
This will echo a large JSON-file. Find the Networks node:
"Networks": {
"bridge": {
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
// other lines omitted
}
}
You will see the network-name as the first child of JSON (bridge in this example) and also the IPAddress. Note theme somewhere.
C. Re-run the pgAdmin4-docker again with --network [NetworkName] parameter; replace [NetworkName] with the name you got in previous step (bridge in this example).
docker run --rm --name pgadmin4 -p 5050:80 --network bridge \
-v $HOME/vols/pgadmin4:/var/lib/pgadmin \
-e 'PGADMIN_DEFAULT_EMAIL=amiry#manexapp.com' \
-e 'PGADMIN_DEFAULT_PASSWORD=12345678' \
-d dpage/pgadmin4
D. Access (or refresh) http://localhost:5050 and login to it. In Add server section, use the IPAddress you got in step B. and you are good to go.
Using
host.docker.internal
instead of localhost

How can I connect to Postgres database in the container via port 5432

I am running a postgres docker container by using the commands below: (reference: https://docs.docker.com/engine/examples/postgresql_service/)
docker build -t eg_postgresql .
docker run --rm -P --name pg_test eg_postgresql
This works but the port number is dynamic. I can connect to the database by giving the port number. (the port I see in docker ps command)
I would like to connect to this docker database from Python so I need a static port number.
I tried the parameters below:
-p 127.0.0.1:5432:5432
-p 5432:5432
In that case, the docker container's port number was set as 5432. However, I could not connect to the database. I get docker user does not exist error message.
What is your advice?
I took the Dockerfile from the link you posted. After building the container with
docker build -t eg_postgresql .
I started the container with
docker run --rm -p 5432:5432 --name pg_test eg_postgresql (which binds localhost port 5432 to the container port 5432)
and then I tried to connect with
psql -h localhost -p 5432 -d docker -U docker --password
It works like a charm. If you get a message that docker user does not exist please double check that all steps from the Dockerfile are executed succesfully during the docker build command as the creation of the docker user is done in the command RUN /etc/init.d/postgresql start &&\
psql --command "CREATE USER docker WITH SUPERUSER PASSWORD 'docker';" &&\
createdb -O docker docker. Make also sure that you have no PostgreSQL server running on your localhost so that you can be sure that you are trying to connect to PostgreSQL inside the container.