I want to connect to the Docker container of postgres by psql.

Asked 2 years ago, Updated 2 years ago, 129 views

Starting postgres server in Docker container

CONTAINER ID NAMES IMAGE COMMAND CREATED STATUS PORTS
b292a7d3de2d backend_container rails "railsc" About an hour ago Up About an hour
b1e3345f625d postgres_container postgres"/docker-entrypoint.s"2 days ago Up 3 hours 5432/tcp

The docker-compose links backend_container to postgres_container, and
from the linked container psql-U postgres-hdb-d postgres
accessible on the .

However, the local environment (the environment hosting the Docker container) is not accessible by psql.
For example, try this command: psql-h$(docker inspect --format"{{.NetworkSettings.IPAddress}}}"postgres_container)-Upostgres-dpostgres

docker exec allows you to log in, but docker exec-it postgres_container psql-U postgres

How can I connect directly from my local environment via psql?
I thought it might be an access control problem, so I tried many things, but it didn't work.

The environment is as follows:

Host: MacBook Pro (OS X El Capitan Version 10.11.6)
Docker version 1.12.3, build 6b644ec
docker-compose version 1.8.1, build878cff1

We would appreciate it if you could lend us your wisdom.I look forward to your kind cooperation.

docker docker-compose

2022-09-30 19:27

3 Answers

You did not seem to specify a port when you ran docker.

at docker-compose.yml
 ports:
  - "5432:5432"

You must specify as shown in .


2022-09-30 19:27

Can I connect even if I specify port?

psql-h$(docker inspect --format"{{.NetworkSettings.IPAddress}}}"postgres_container) -U postgres-d postgres-p532


2022-09-30 19:27

I have encountered a similar event, so I will share the countermeasures.
(I don't know if this will work.)

■Cause
 Invalid destination host.

If you docker-compose, it will become a closed NW, so
 The IP of the postgresql container visible from the host (for example, localhost:5432), and
 The IP of the postgresql container seen from the above NW is different.
 
■ Countermeasures
 1. Do the following to find the IP of the postgresql container (like "IPAddress")
  $docker inspect

2. Connect with the postgresql client by specifying the above IP.

That's all.


2022-09-30 19:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.