0

I can't access the instance of Postgres running on a docker container from the PS. I seem to access a different instance... so how can I connect to the correct one?

Here I run the image:

docker run --name postgres-0 -e POSTGRES_PASSWORD=password -d -p 5432:5432 postgres:latest
260e6be6f6a9a1aa3050d6564ca59bffdcc691145902fa0e85a6eace620550d8

Here I check the container status:

docker ps -a
CONTAINER ID   IMAGE             COMMAND                  CREATED          STATUS          PORTS                    NAMES
260e6be6f6a9   postgres:latest   "docker-entrypoint.s…"   18 seconds ago   Up 17 seconds   0.0.0.0:5432->5432/tcp   postgres-0

Here I bash into the container:

docker exec -it postgres-0 bash

Here I start psql on the container:

root@260e6be6f6a9:/# psql -U postgres

Here I create a new database in the postgres instance of the container:

postgres=# CREATE DATABASE test;
CREATE DATABASE

Here I check the database I just created is actually there:

postgres=# \l

enter image description here

It is actually there.

Meanwhile on another PS window...

psql -U localhost -p 5432 -U postgres

When I check for the database present:

postgres=# \l

I get:

enter image description here

All of this on:

Windows 10 --> psql (PostgreSQL) 14.4,

Docker version 20.10.16, build aa7e414, --> psql (PostgreSQL) 14.3 (Debian 14.3-1.pgdg110+1)

IDK
  • 359
  • 1
  • 16
  • Please [edit] your question to include any source code, debugging information, console output, _etc._ in text format. – David Maze Jun 21 '22 at 19:23
  • There are different databases - looking on collates. Additionally version of postgresql is different. Try to expose postgres form docker on different port and use localhost for connection - but with new port. Or try to shut down local postgres – Kadet Jun 21 '22 at 19:57

1 Answers1

0

Check your networks

docker network ls

Check details for network used for your container

docker network inspect <network_name_for_container>

Part of output for this command will look like this

"Containers": {
    "cedf7a860c5c593ced145ae4cc13340": {
         "Name": "reppgadmin",
         "EndpointID": "9313962e8c86a9bd317605f9de5d32a44fa",
         "MacAddress": "02:42:ac:12:00:02",
         "IPv4Address": "172.18.0.2/16",
         "IPv6Address": ""
 },

IP of container can be found in Containers->your container -> IP4Address node

But after checking - connection to postgresql should be possible by using localhost - try to change port to which postgresql is mapped because now it looks like docker instance (version 14.3-1) can interfere with other instance (version 14.4).

Kadet
  • 1,344
  • 3
  • 10
  • yeah I found it! but... I still can't connect look at edit – IDK Jun 21 '22 at 19:12
  • It was my the other try: -h 172.17.0.2 -p 5432 (5432 is the port exposed on container as you can see in 1° image) – IDK Jun 21 '22 at 19:19
  • You should never need to know this IP address. It's not usable from outside Docker on most platforms, and between containers the host name `reppgadmin` will work so long as both containers are on the same network. – David Maze Jun 21 '22 at 19:24
  • After checking with my docker I've updated answer. It looks like you have another version of postgresql (14.4) on this port already. In containner you have 14.3-1. Try to use another port or shut down this local postgresql – Kadet Jun 21 '22 at 19:42
  • Yeah! It actually worked, I shut down the local postgresql ! – IDK Jun 21 '22 at 19:56