Home > Software engineering >  Can't access Postgres instance inside docker
Can't access Postgres instance inside docker

Time:06-22

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)

CodePudding user response:

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).

  • Related