I am trying to connect to my postgres instance which is running at port 5001 but the connection code is returning the error: [error] failed to initialize database, got error failed to connect to host=localhost user=admin database=contactsdb
: failed to receive message (unexpected EOF)
我试图连接到我的postgres实例,该实例在端口5001运行,但连接代码返回错误:[错误]无法初始化数据库,得到错误无法连接到主机=localhost用户=admin数据库=contactsdb:无法接收消息(意外错误)
Podman script
波德曼脚本
podman run --name mypostgres -p 50001:5001 -e POSTGRES_DB=contactsdb -e POSTGRES_USER=admin -e POSTGRES_PASSWORD=admin123 -d postgres
Here's my connection code
这是我的转接码
dsn:="host=localhost user=admin password=admin123 dbname=contactsdb port=50001"
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err!=nil{
fmt.Println("Could not Connect")
} else{
fmt.Println("Successfully connected",db)
}
Here's my postgres container:
这是我的Postgres容器:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a238196fb656 docker.io/library/mysql:latest mysqld 8 hours ago Up 8 hours 0.0.0.0:33406->3306/tcp mysq2
4ee241546da5 docker.io/library/adminer:latest php -S [::]:8080 ... 8 hours ago Up 8 hours 0.0.0.0:48088->8080/tcp adminui
e7bee47ab8ac docker.io/library/postgres:latest postgres About an hour ago Up About an hour 0.0.0.0:50001->5001/tcp mypostgres
更多回答
优秀答案推荐
You need to expose the container to the host’s network by adding --network host
to your podman run
command.
您需要通过将--Network host添加到您的podman run命令来向主机的网络公开容器。
更多回答
which port should i connect to for my connection string because it's not showing the port in the podman inspect
我应该为我的连接字符串连接到哪个端口,因为它没有在Podman检查中显示端口
我是一名优秀的程序员,十分优秀!