docker-compose.yml:
version: "3.9"
services:
postgres:
image: postgres:15
environment:
POSTGRES_DB: "db"
POSTGRES_USER: "user"
POSTGRES_PASSWORD: "password"
ports:
- "5432:5432"
让我们运行:
docker-compose up
我认为在完成操作后,我将能够postgress使用localhost. (我所说的连接,是指不进入docker容器的情况下进行连接)但是,这种方法不行;连接的时候会报错
connection to server at "localhost" (::1), port 5432 failed: FATAL: role "user" does not exist
经过谷歌搜索,我找到了正确的连接方式 - 你不需要使用localhost,而是需要使用通过命令获取的地址
ifconfig -u | grep 'inet ' | grep -v 127.0.0.1 | cut -d\ -f2 | head -1
虽然我能够联系上,但我对情况产生了误解。为什么连接不起作用localhost?为什么我描述的第二种方法有效?无论如何,这个命令有什么意义呢ifconfig -u | grep 'inet ' | grep -v 127.0.0.1 | cut -d\ -f2 | head -1?
我有Mac系统
要连接到外部创建的数据库
docker-e,您需要将端口转发到服务docker-compose:现在可以连接到容器内的数据库 -
localhost:13000测试:
输出: